]> granicus.if.org Git - graphviz/commitdiff
rewrite agxbputc as a function
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 1 Mar 2021 04:30:59 +0000 (20:30 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 7 Mar 2021 19:16:25 +0000 (11:16 -0800)
This solves an issue where the macro implementation did not sufficiently bracket
its arguments. Fixes #1814.

CHANGELOG.md
lib/cgraph/agxbuf.h
lib/common/htmllex.c
lib/common/utils.c
lib/xdot/xdot.c
plugin/core/gvrender_core_json.c
plugin/pango/gvgetfontlist_pango.c

index 9edd5002b7be44529de5888b6d9f9f2de9876aac..36442c97aae845d57befc3798b00cac32c67d558 100644 (file)
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - memory leak in ANN bridge
 - gvpr on Windows does not support absolute paths #1780
 - buffer overflow in unflatten
+- agxbputc macro does not bracket its arguments #1814
 
 ## [2.46.1] - 2021-02-13
 
index 4692cf91f0af3cc1ae0a07b0ba0592449b4e2cc1..3ed3c736ea5d405a1c37dd8bfae8a16169f5cb95 100644 (file)
@@ -88,7 +88,15 @@ extern "C" {
  * Add character to buffer.
  *  int agxbputc(agxbuf*, char)
  */
-#define agxbputc(X,C) ((((X)->ptr >= (X)->eptr) ? agxbmore(X,1) : 0), (void)(*(X)->ptr++ = ((unsigned char)C)))
+static inline int agxbputc(agxbuf * xb, char c) {
+  if (xb->ptr >= xb->eptr) {
+    if (agxbmore(xb, 1) != 0) {
+      return -1;
+    }
+  }
+  *xb->ptr++ = (unsigned char)c;
+  return 0;
+}
 
 /* agxbuse:
  * Null-terminates buffer; resets and returns pointer to data. The buffer is
index db7687314d4e09b665604770e23a4717f9cfde6d..866e9af205682d53baeb947e6edb9febd0488daf 100644 (file)
@@ -735,7 +735,7 @@ static void characterData(void *user, const char *s, int length)
            c = *s++;
            if (c >= ' ') {
                cnt++;
-               agxbputc(state.xb, c);
+               agxbputc(state.xb, (char)c);
            }
        }
        if (cnt) state.tok = T_string;
index a811d39d891ca529db0364a0106abc1aac4440c3..e463eab57adfdda26f1a4e2da3fc8a19f0c51a97 100644 (file)
@@ -1515,12 +1515,12 @@ char* htmlEntityUTF8 (char* s, graph_t* g)
                    if (v < 0x7F) /* entity needs 1 byte in UTF8 */
                        c = v;
                    else if (v < 0x07FF) { /* entity needs 2 bytes in UTF8 */
-                       agxbputc(&xb, (v >> 6) | 0xC0);
+                       agxbputc(&xb, (char)((v >> 6) | 0xC0));
                        c = (v & 0x3F) | 0x80;
                    }
                    else { /* entity needs 3 bytes in UTF8 */
-                       agxbputc(&xb, (v >> 12) | 0xE0);
-                       agxbputc(&xb, ((v >> 6) & 0x3F) | 0x80);
+                       agxbputc(&xb, (char)((v >> 12) | 0xE0));
+                       agxbputc(&xb, (char)(((v >> 6) & 0x3F) | 0x80));
                        c = (v & 0x3F) | 0x80;
                    }
                    }
@@ -1528,7 +1528,7 @@ char* htmlEntityUTF8 (char* s, graph_t* g)
         else /* copy n byte UTF8 characters */
             for (ui = 0; ui < uc; ++ui)
                 if ((*s & 0xC0) == 0x80) {
-                    agxbputc(&xb, c);
+                    agxbputc(&xb, (char)c);
                     c = *(unsigned char*)s++;
                 }
                 else {
@@ -1539,7 +1539,7 @@ char* htmlEntityUTF8 (char* s, graph_t* g)
                            c = cvtAndAppend (c, &xb);
                     break;
                    }
-           agxbputc(&xb, c);
+        agxbputc(&xb, (char)c);
     }
     ns = agxbdisown(&xb);
     agxbfree(&xb);
@@ -1568,15 +1568,15 @@ char* latin1ToUTF8 (char* s)
            if (!v) v = '&';
         }
        if (v < 0x7F)
-           agxbputc(&xb, v);
+           agxbputc(&xb, (char)v);
        else if (v < 0x07FF) {
-           agxbputc(&xb, (v >> 6) | 0xC0);
-           agxbputc(&xb, (v & 0x3F) | 0x80);
+           agxbputc(&xb, (char)((v >> 6) | 0xC0));
+           agxbputc(&xb, (char)((v & 0x3F) | 0x80));
        }
        else {
-           agxbputc(&xb, (v >> 12) | 0xE0);
-           agxbputc(&xb, ((v >> 6) & 0x3F) | 0x80);
-           agxbputc(&xb, (v & 0x3F) | 0x80);
+           agxbputc(&xb, (char)((v >> 12) | 0xE0));
+           agxbputc(&xb, (char)(((v >> 6) & 0x3F) | 0x80));
+           agxbputc(&xb, (char)((v & 0x3F) | 0x80));
        }
     }
     ns = agxbdisown(&xb);
@@ -1601,12 +1601,12 @@ utf8ToLatin1 (char* s)
 
     while ((c = *(unsigned char*)s++)) {
        if (c < 0x7F)
-           agxbputc(&xb, c);
+           agxbputc(&xb, (char)c);
        else {
            outc = (c & 0x03) << 6;
            c = *(unsigned char*)s++;
            outc = outc | (c & 0x3F);
-           agxbputc(&xb, outc);
+           agxbputc(&xb, (char)outc);
        }
     }
     ns = agxbdisown(&xb);
index 131eac37a8c972e85501e02d15e074c0fa553d28..9a24ef5c46a122a005d1359edc481ffa32defece 100644 (file)
@@ -23,8 +23,6 @@ typedef struct {
     int dyna;                  /* true if buffer is malloc'ed */
 } agxbuf;
 
-#define agxbputc(X,C) ((((X)->ptr >= (X)->eptr) ? agxbmore(X,1) : 0), \
-          (void)(*(X)->ptr++ = ((unsigned char)C)))
 #define agxbuse(X) (agxbputc(X,'\0'),(char*)((X)->ptr = (X)->buf))
 
 static void agxbinit(agxbuf * xb, unsigned int hint, unsigned char *init)
@@ -77,6 +75,16 @@ static int agxbput(char *s, agxbuf * xb)
     return ssz;
 }
 
+static int agxbputc(agxbuf * xb, char c) {
+  if (xb->ptr >= xb->eptr) {
+    if (agxbmore(xb, 1) != 0) {
+      return -1;
+    }
+  }
+  *xb->ptr++ = (unsigned char)c;
+  return 0;
+}
+
 /* agxbfree:
  * Free any malloced resources.
  */
index 054489a6bb0fba12390adf55a166ef04d1eddbd9..b4570a7093ea00968063c82b04b1738f4aa0c677 100644 (file)
@@ -82,7 +82,7 @@ static char* stoj (char* ins, state_t* sp)
     char* s;
     char* input;
     static agxbuf xb;
-    unsigned char c;
+    char c;
 
     if (sp->isLatin)
        input = latin1ToUTF8 (ins);
index a194e42ea21e59afbda481c6467f4a361d61aef9..cee44951ff2ec2067517832c6015df33194c1625 100644 (file)
@@ -420,7 +420,7 @@ static void copyUpper (agxbuf* xb, char* s)
     int c;
 
     while ((c = *s++))
-       (void)agxbputc (xb, toupper(c));
+       (void)agxbputc (xb, (char)toupper(c));
 }
 
 /* Returns the font corresponding to a Graphviz PS font.