From: Matthew Fernandez Date: Thu, 20 May 2021 00:07:11 +0000 (-0700) Subject: remove unused string_split X-Git-Tag: 2.47.3~26^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a39926fa8ac8dd2ee046d15a0cf4df443b34141f;p=graphviz remove unused string_split Apart from being unused, this function has multiple issues: 1. Comment typo “separater.” 2. Comment discusses a path returning 1 that does not exist. 3. Useless return value. 4. The last store to len in the last loop is dead. This variable is unused after this point because the containing loop is exited and it is unused in the remainder of the function. 5. The two mallocs in the last loop over-allocate by one byte. The len variable tracking the length of strings being constructed here was incremented *past* the '\0' terminator in the previous line. Thus the length of the string including '\0' is len, not len + 1. 6. The allocations in the loop are essentially doing an open-coded strdup (or even more efficiently, strndup). 7. Both the large static buffer swork and the optional larger allocation into swork1 are unnecessary. Tracking offset and length in s would allow using the original memory as a source to any strndup/strncpy. 8. The ss allocation reserves one entry too many. The number of tokens is ntokens, not ntokens + 1, and this array is not zeroed on allocation so the last entry merely contains garbage. --- diff --git a/cmd/gvmap/gvmap.c b/cmd/gvmap/gvmap.c index 06b38c1a7..3b860624d 100644 --- a/cmd/gvmap/gvmap.c +++ b/cmd/gvmap/gvmap.c @@ -29,9 +29,7 @@ #include #include -enum {maxlen = 10000000}; enum {MAX_GRPS = 10000}; -static char swork[maxlen]; typedef struct { char* cmd; @@ -62,57 +60,6 @@ typedef struct { int seed; /* seed used to calculate Fiedler vector */ } params_t; -int string_split(char *s, char sp, char ***ss0, int *ntokens0){ - /* give a string s ending with a \0, split into an array of strings using separater sp, - with \0 inserted. Return 0 if successful, 1 if length of string exceed buffer size */ - int ntokens = 0, len = 0; - - int i; - char **ss; - char *swork1 = swork; - if (strlen(s) > maxlen) { - swork1 = MALLOC(sizeof(char)*maxlen); - } - - for (i = 0; i < strlen(s); i++){ - if (s[i] == sp){ - ntokens++; - } else if (s[i] == '\n' || s[i]== EOF){ - ntokens++; - break; - } - } - - ss = malloc(sizeof(char*)*(ntokens+1)); - ntokens = 0; - for (i = 0; i < strlen(s); i++){ - if (s[i] == sp){ - swork1[len++] = '\0'; - ss[ntokens] = malloc(sizeof(char)*(len+1)); - strcpy(ss[ntokens], swork1); - len = 0; - ntokens++; - continue; - } else if (s[i] == '\n' || s[i] == EOF){ - swork1[len++] = '\0'; - ss[ntokens] = malloc(sizeof(char)*(len+1)); - strcpy(ss[ntokens], swork1); - len = 0; - ntokens++; - break; - } - swork1[len++] = s[i]; - } - - *ntokens0 = ntokens; - - *ss0 = ss; - - if (swork1 != swork) FREE(swork1); - return 0; - -} - static char* usestr = " where graphfile must contain node positions, and widths and heights for each node. No overlap between nodes should be present. Acceptable options are: \n\ -a k - average number of artificial points added along the bounding box of the labels. If < 0, a suitable value is selected automatically. (-1)\n\