From: Art Cancro Date: Thu, 4 May 2023 22:14:10 +0000 (-0400) Subject: tools.c: bugfixes to trim functions (Phil Slack) X-Git-Tag: v976~10 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=e9f3ccdaa47338e69818ef5c52ff2cac0767c08c tools.c: bugfixes to trim functions (Phil Slack) libcitadel/lib/tools.c stripout() Strips too many characters. Causes incorrect From address on inbound and probably other places. stripout(“Foobar”) = Fooba. Should be Foobar Mangles multiple boundaries. Should strip using outer boundaries. stripout(“ABC()(DEF)()GHI”) = ABC()(DEFGHI. Should return ABCGHI. stripallbut() Handle outer boundaries like stripout() stripallbut(“ABC()(DEF)()GHI”) returns unchanged. Should return )(DEF)( Code does a stderr flush. Not sure why, but left it in. string_trimlen() Removed function. Not used anywhere in the code and less efficient than string_trim() --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index d9595f508..48e615850 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -430,7 +430,6 @@ void CtdlMakeTempFileName(char *name, int len); char *rfc2047encode(const char *line, long length); int is_msg_in_mset(const char *mset, long msgnum); int pattern2(char *search, char *patn); -void string_trimlen(char *, int *); char *html_to_ascii(const char *inputmsg, int msglen, int screenwidth, int ansi); void LoadEntityList(char *FileName); void utf8ify_rfc822_string(char *buf); diff --git a/libcitadel/lib/tools.c b/libcitadel/lib/tools.c index 7523b17e3..2b5066d95 100644 --- a/libcitadel/lib/tools.c +++ b/libcitadel/lib/tools.c @@ -431,53 +431,60 @@ const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retle // Strip a boundarized substring out of a string (for example, remove parentheses and anything inside them). int stripout(char *str, char leftboundary, char rightboundary) { - int a; - int lb = (-1); - int rb = (-1); - - for (a = 0; a < strlen(str); ++a) { - if (str[a] == leftboundary) lb = a; - if (str[a] == rightboundary) rb = a; - } - - if ( (lb > 0) && (rb > lb) ) { - strcpy(&str[lb - 1], &str[rb + 1]); - return 1; - } - - else if ( (lb == 0) && (rb > lb) ) { - strcpy(str, &str[rb + 1]); - return 1; - } - return 0; -} + long lb = (-1); + long rb = (-1); + + if (!str) { + return 0; + } + for (int a = 0; str[a]; ++a) { + if ((lb==-1) && (str[a] == leftboundary)) { + lb = a; + } else if (str[a] == rightboundary) { + rb = a; + } + } + + if ((lb==-1) || (rb <= lb)) { + return 0; + } + + strcpy(str + lb, str + rb + 1); + return 1; +} // Reduce a string down to a boundarized substring (for example, remove // parentheses and anything outside them). long stripallbut(char *str, char leftboundary, char rightboundary) { - long len = 0; - - char *lb = NULL; - char *rb = NULL; - - lb = strrchr(str, leftboundary); - if (lb != NULL) { - ++lb; - rb = strchr(str, rightboundary); - if ((rb != NULL) && (rb >= lb)) { - *rb = 0; - fflush(stderr); - len = (long)rb - (long)lb; - memmove(str, lb, len); - str[len] = 0; - return(len); + long lb = (-1); + long rb = (-1); + long orig_len = 0; + + if (!str) { + return 0; + } + + while (str[orig_len]) { + if ((lb==-1) && (str[orig_len] == leftboundary)) { + lb = orig_len; + } else if (str[orig_len] == rightboundary) { + rb = orig_len; } + orig_len++; } - return (long)strlen(str); -} + if ((lb==-1) || (rb <= lb)) { + return orig_len; + } + fflush(stderr); + + long new_len = rb - lb - 1; + memmove(str, str + lb + 1, new_len); + str[new_len] = 0; + return new_len; +} char *myfgets(char *s, int size, FILE *stream) { char *ret = fgets(s, size, stream); @@ -808,27 +815,6 @@ int pattern2(char *search, char *patn) { } -/* - * Strip leading and trailing spaces from a string; with premeasured and adjusted length. - * buf - the string to modify - * len - length of the string. - */ -void string_trimlen(char *buf, int *len) { - int delta = 0; - if (*len == 0) return; - while ((*len > delta) && (isspace(buf[delta]))){ - delta ++; - } - memmove (buf, &buf[delta], *len - delta + 1); - (*len) -=delta; - - if (*len == 0) return; - while (isspace(buf[(*len) - 1])){ - buf[--(*len)] = '\0'; - } -} - - /* * Convert all whitespace characters in a supplied string to underscores */