Browse code

bb12370 - cli_strndup and other str* replacements must be built and exported for every OS to be used outside of libclamav on systems that don't have the original functions (e.g. strndup). This commit renames the macros to be uppercase, renames the replacement functions to be preceeded with two understores (e.g. __cli_strndup), and removes the ifdef's so that they are built regardless, because there are no ifdefs in libclamav.map.

Micah Snyder authored on 2019/08/23 05:51:01
Showing 13 changed files
... ...
@@ -104,7 +104,7 @@ int localserver(const struct optstruct *opts)
104 104
         pos     = server.sun_path + strlen(server.sun_path);
105 105
         while (pos != server.sun_path) {
106 106
             if (*pos == '/') {
107
-                sockdir = cli_strndup(server.sun_path, strlen(server.sun_path) - cnt);
107
+                sockdir = CLI_STRNDUP(server.sun_path, strlen(server.sun_path) - cnt);
108 108
                 break;
109 109
             } else {
110 110
                 pos--;
... ...
@@ -506,7 +506,7 @@ static int onas_add_hashnode_child(struct onas_hnode *node, const char *dirname)
506 506
     if (!child) return CL_EMEM;
507 507
 
508 508
     size_t n       = strlen(dirname);
509
-    child->dirname = cli_strndup(dirname, n);
509
+    child->dirname = CLI_STRNDUP(dirname, n);
510 510
 
511 511
     onas_add_listnode(node->childtail, child);
512 512
 
... ...
@@ -577,7 +577,7 @@ inline static char *onas_get_parent(const char *pathname, size_t len)
577 577
         idx++;
578 578
     }
579 579
 
580
-    ret = cli_strndup(pathname, idx);
580
+    ret = CLI_STRNDUP(pathname, idx);
581 581
     if (!ret) {
582 582
         errno = ENOMEM;
583 583
         return NULL;
... ...
@@ -701,7 +701,7 @@ int onas_ht_add_hierarchy(struct onas_ht *ht, const char *pathname)
701 701
                 }
702 702
 
703 703
                 hnode->pathlen  = curr->fts_pathlen;
704
-                hnode->pathname = cli_strndup(curr->fts_path, hnode->pathlen);
704
+                hnode->pathname = CLI_STRNDUP(curr->fts_path, hnode->pathlen);
705 705
 
706 706
                 hnode->prnt_pathname = onas_get_parent(hnode->pathname, hnode->pathlen);
707 707
                 if (hnode->prnt_pathname)
... ...
@@ -897,7 +897,7 @@ static cl_error_t egg_parse_comment_header(const uint8_t* index, size_t size, ex
897 897
         }
898 898
     } else {
899 899
         /* Should already be UTF-8. Use as-is.. */
900
-        comment_utf8 = cli_strndup((char*)index, size);
900
+        comment_utf8 = CLI_STRNDUP((char*)index, size);
901 901
         if (NULL == comment_utf8) {
902 902
             cli_dbgmsg("egg_parse_comment_header: failed to allocate comment buffer.\n");
903 903
             status = CL_EMEM;
... ...
@@ -1453,7 +1453,7 @@ static cl_error_t egg_parse_file_extra_field(egg_handle* handle, egg_file* eggFi
1453 1453
                 }
1454 1454
             } else {
1455 1455
                 /* Should already be UTF-8. Use as-is.. */
1456
-                name_utf8 = cli_strndup((char*)index, name_size);
1456
+                name_utf8 = CLI_STRNDUP((char*)index, name_size);
1457 1457
                 if (NULL == name_utf8) {
1458 1458
                     cli_dbgmsg("egg_parse_file_extra_field: failed to allocate name buffer.\n");
1459 1459
                     status = CL_EMEM;
... ...
@@ -257,6 +257,11 @@ CLAMAV_PRIVATE {
257 257
     cl_base64_decode;
258 258
     cl_base64_encode;
259 259
     cli_sanitize_filepath;
260
+
261
+    __cli_strcasestr;
262
+    __cli_strndup;
263
+    __cli_strnlen;
264
+    __cli_strnstr;
260 265
   local:
261 266
     *;
262 267
 };
... ...
@@ -284,13 +284,13 @@ static int cli_ac_addpatt_recursive(struct cli_matcher *root, struct cli_ac_patt
284 284
      * it's why this function was re-written to be recursive
285 285
      */
286 286
     if ((pattern->sigopts & ACPATT_OPTION_NOCASE) && isalpha(pattern->pattern[i] & 0xff)) {
287
-        next = pt->trans[cli_nocasei((unsigned char)(pattern->pattern[i] & 0xff))];
287
+        next = pt->trans[CLI_NOCASEI((unsigned char)(pattern->pattern[i] & 0xff))];
288 288
         if (!next)
289 289
             next = add_new_node(root, i, len);
290 290
         if (!next)
291 291
             return CL_EMEM;
292 292
         else
293
-            pt->trans[cli_nocasei((unsigned char)(pattern->pattern[i] & 0xff))] = next;
293
+            pt->trans[CLI_NOCASEI((unsigned char)(pattern->pattern[i] & 0xff))] = next;
294 294
 
295 295
         if ((ret = cli_ac_addpatt_recursive(root, pattern, next, i + 1, len)) != CL_SUCCESS)
296 296
             return ret;
... ...
@@ -878,7 +878,7 @@ static int ac_forward_match_branch(const unsigned char *buffer, uint32_t bp, uin
878 878
             break;                                                   \
879 879
                                                                      \
880 880
         case CLI_MATCH_NOCASE:                                       \
881
-            if ((unsigned char)(p & 0xff) != cli_nocase(b))          \
881
+            if ((unsigned char)(p & 0xff) != CLI_NOCASE(b))          \
882 882
                 match = 0;                                           \
883 883
             break;                                                   \
884 884
                                                                      \
... ...
@@ -909,7 +909,7 @@ static int ac_forward_match_branch(const unsigned char *buffer, uint32_t bp, uin
909 909
             break;                                                                            \
910 910
                                                                                               \
911 911
         case CLI_MATCH_NOCASE:                                                                \
912
-            if ((unsigned char)(p & 0xff) != cli_nocase(b))                                   \
912
+            if ((unsigned char)(p & 0xff) != CLI_NOCASE(b))                                   \
913 913
                 match = 0;                                                                    \
914 914
             break;                                                                            \
915 915
                                                                                               \
... ...
@@ -2164,7 +2164,7 @@ inline static int ac_addspecial_add_alt_node(const char *subexpr, uint8_t sigopt
2164 2164
     if (sigopts & ACPATT_OPTION_NOCASE) {
2165 2165
         for (i = 0; i < newnode->len; ++i)
2166 2166
             if ((newnode->str[i] & CLI_MATCH_METADATA) == CLI_MATCH_CHAR) {
2167
-                newnode->str[i] = cli_nocase(newnode->str[i] & 0xff);
2167
+                newnode->str[i] = CLI_NOCASE(newnode->str[i] & 0xff);
2168 2168
                 newnode->str[i] += CLI_MATCH_NOCASE;
2169 2169
             }
2170 2170
     }
... ...
@@ -2497,7 +2497,7 @@ cl_error_t cli_ac_addsig(struct cli_matcher *root, const char *virname, const ch
2497 2497
                 }
2498 2498
 
2499 2499
                 if ((sigopts & ACPATT_OPTION_NOCASE) && ((*dec & CLI_MATCH_METADATA) == CLI_MATCH_CHAR))
2500
-                    new->ch[i] = cli_nocase(*dec) | CLI_MATCH_NOCASE;
2500
+                    new->ch[i] = CLI_NOCASE(*dec) | CLI_MATCH_NOCASE;
2501 2501
                 else
2502 2502
                     new->ch[i] = *dec;
2503 2503
                 free(dec);
... ...
@@ -2513,7 +2513,7 @@ cl_error_t cli_ac_addsig(struct cli_matcher *root, const char *virname, const ch
2513 2513
                 }
2514 2514
 
2515 2515
                 if ((sigopts & ACPATT_OPTION_NOCASE) && ((*dec & CLI_MATCH_METADATA) == CLI_MATCH_CHAR))
2516
-                    new->ch[i] = cli_nocase(*dec) | CLI_MATCH_NOCASE;
2516
+                    new->ch[i] = CLI_NOCASE(*dec) | CLI_MATCH_NOCASE;
2517 2517
                 else
2518 2518
                     new->ch[i] = *dec;
2519 2519
                 free(dec);
... ...
@@ -2717,7 +2717,7 @@ cl_error_t cli_ac_addsig(struct cli_matcher *root, const char *virname, const ch
2717 2717
     if (sigopts & ACPATT_OPTION_NOCASE) {
2718 2718
         for (i = 0; i < new->length[0]; i++)
2719 2719
             if ((new->pattern[i] & CLI_MATCH_METADATA) == CLI_MATCH_CHAR) {
2720
-                new->pattern[i] = cli_nocase(new->pattern[i] & 0xff);
2720
+                new->pattern[i] = CLI_NOCASE(new->pattern[i] & 0xff);
2721 2721
                 new->pattern[i] += CLI_MATCH_NOCASE;
2722 2722
             }
2723 2723
     }
... ...
@@ -141,7 +141,7 @@ cl_error_t cli_bcomp_addpatt(struct cli_matcher *root, const char *virname, cons
141 141
     }
142 142
 
143 143
     /* make a working copy of the param buffer */
144
-    buf = cli_strndup(buf_start, len);
144
+    buf = CLI_STRNDUP(buf_start, len);
145 145
 
146 146
     /* break up the new param buffer into its component strings and verify we have exactly 3 */
147 147
     toks = cli_strtokenize(buf, '#', 3 + 1, tokens);
... ...
@@ -2220,7 +2220,7 @@ parseEmailBody(message *messageIn, text *textIn, mbox_ctx *mctx, unsigned int re
2220 2220
 					 * Don't bother with text/plain or
2221 2221
 					 * text/html
2222 2222
 					 */
2223
-                    if (cli_strcasestr(s, "text/plain") != NULL)
2223
+                    if (CLI_STRCASESTR(s, "text/plain") != NULL)
2224 2224
                         /*
2225 2225
 						 * Don't bother to save the
2226 2226
 						 * unuseful part, read past
... ...
@@ -2230,7 +2230,7 @@ parseEmailBody(message *messageIn, text *textIn, mbox_ctx *mctx, unsigned int re
2230 2230
 						 */
2231 2231
                         continue;
2232 2232
                     if ((!doPhishingScan) &&
2233
-                        (cli_strcasestr(s, "text/html") != NULL))
2233
+                        (CLI_STRCASESTR(s, "text/html") != NULL))
2234 2234
                         continue;
2235 2235
                     break;
2236 2236
                 }
... ...
@@ -812,7 +812,7 @@ char *cli_mpool_strndup(mpool_t *mp, const char *s, size_t n)
812 812
         return NULL;
813 813
     }
814 814
 
815
-    strsz = cli_strnlen(s, n) + 1;
815
+    strsz = CLI_STRNLEN(s, n) + 1;
816 816
     alloc = mpool_malloc(mp, strsz);
817 817
     if (!alloc)
818 818
         cli_errmsg("cli_mpool_strndup(): Can't allocate memory (%lu bytes).\n", (unsigned long)strsz);
... ...
@@ -933,7 +933,7 @@ char *cli_sanitize_filepath(const char *filepath, size_t filepath_len)
933 933
              * Is not "/", "./", or "../".
934 934
              */
935 935
             /* Find the next path separator. */
936
-            next_pathsep = cli_strnstr(filepath + index, PATHSEP, filepath_len - index);
936
+            next_pathsep = CLI_STRNSTR(filepath + index, PATHSEP, filepath_len - index);
937 937
             if (NULL == next_pathsep) {
938 938
                 /* No more path separators, copy the rest (filename) into the sanitized path */
939 939
                 strncpy(sanitized_filepath + sanitized_index, filepath + index, filepath_len - index);
... ...
@@ -1128,7 +1128,7 @@ cl_error_t cli_get_filepath_from_filedesc(int desc, char **filepath)
1128 1128
     /* Success. Add null terminator */
1129 1129
     fname[linksz] = '\0';
1130 1130
 
1131
-    *filepath = cli_strndup(fname, cli_strnlen(fname, PATH_MAX));
1131
+    *filepath = CLI_STRNDUP(fname, CLI_STRNLEN(fname, PATH_MAX));
1132 1132
     if (NULL == *filepath) {
1133 1133
         cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate memory to store filename\n");
1134 1134
         status = CL_EMEM;
... ...
@@ -1145,7 +1145,7 @@ cl_error_t cli_get_filepath_from_filedesc(int desc, char **filepath)
1145 1145
         goto done;
1146 1146
     }
1147 1147
 
1148
-    *filepath = cli_strndup(fname, cli_strnlen(fname, PATH_MAX));
1148
+    *filepath = CLI_STRNDUP(fname, CLI_STRNLEN(fname, PATH_MAX));
1149 1149
     if (NULL == *filepath) {
1150 1150
         cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate memory to store filename\n");
1151 1151
         status = CL_EMEM;
... ...
@@ -2348,7 +2348,7 @@ static inline int hash_impfns(cli_ctx *ctx, void **hashctx, uint32_t *impsz, str
2348 2348
                 if (!ret) {
2349 2349
                     /* Hint field is a uint16_t and precedes the Name field */
2350 2350
                     if ((buffer = fmap_need_off_once(map, offset + sizeof(uint16_t), MIN(PE_MAXNAMESIZE, fsize - offset))) != NULL) {
2351
-                        funcname = cli_strndup(buffer, MIN(PE_MAXNAMESIZE, fsize - offset));
2351
+                        funcname = CLI_STRNDUP(buffer, MIN(PE_MAXNAMESIZE, fsize - offset));
2352 2352
                         if (funcname == NULL) {
2353 2353
                             cli_dbgmsg("scan_pe: cannot duplicate function name\n");
2354 2354
                             return CL_EMEM;
... ...
@@ -2384,7 +2384,7 @@ static inline int hash_impfns(cli_ctx *ctx, void **hashctx, uint32_t *impsz, str
2384 2384
                 if (!err) {
2385 2385
                     /* Hint field is a uint16_t and precedes the Name field */
2386 2386
                     if ((buffer = fmap_need_off_once(map, offset + sizeof(uint16_t), MIN(PE_MAXNAMESIZE, fsize - offset))) != NULL) {
2387
-                        funcname = cli_strndup(buffer, MIN(PE_MAXNAMESIZE, fsize - offset));
2387
+                        funcname = CLI_STRNDUP(buffer, MIN(PE_MAXNAMESIZE, fsize - offset));
2388 2388
                         if (funcname == NULL) {
2389 2389
                             cli_dbgmsg("scan_pe: cannot duplicate function name\n");
2390 2390
                             return CL_EMEM;
... ...
@@ -2508,7 +2508,7 @@ static unsigned int hash_imptbl(cli_ctx *ctx, unsigned char **digest, uint32_t *
2508 2508
             goto hash_imptbl_end;
2509 2509
         }
2510 2510
 
2511
-        dllname = cli_strndup(buffer, MIN(PE_MAXNAMESIZE, fsize - offset));
2511
+        dllname = CLI_STRNDUP(buffer, MIN(PE_MAXNAMESIZE, fsize - offset));
2512 2512
         if (dllname == NULL) {
2513 2513
             cli_dbgmsg("scan_pe: cannot duplicate dll name\n");
2514 2514
             ret = CL_EMEM;
... ...
@@ -454,36 +454,23 @@ char *cli_strrcpy(char *dest, const char *source) /* by NJH */
454 454
     return --dest;
455 455
 }
456 456
 
457
-#ifndef HAVE_STRCASESTR
458
-const char *cli_strcasestr(const char *a, const char *b)
457
+const char *__cli_strcasestr(const char *haystack, const char *needle)
459 458
 {
460 459
     size_t l;
461 460
     char f[3];
462
-    const size_t strlen_a = strlen(a);
463
-    const size_t strlen_b = strlen(b);
461
+    const size_t strlen_a = strlen(haystack);
462
+    const size_t strlen_b = strlen(needle);
464 463
 
465
-    f[0] = tolower(*b);
466
-    f[1] = toupper(*b);
464
+    f[0] = tolower(*needle);
465
+    f[1] = toupper(*needle);
467 466
     f[2] = '\0';
468
-    for (l = strcspn(a, f); l != strlen_a; l += strcspn(a + l + 1, f) + 1)
469
-        if (strncasecmp(a + l, b, strlen_b) == 0)
470
-            return (a + l);
467
+    for (l = strcspn(haystack, f); l != strlen_a; l += strcspn(haystack + l + 1, f) + 1)
468
+        if (strncasecmp(haystack + l, needle, strlen_b) == 0)
469
+            return (haystack + l);
471 470
     return (NULL);
472 471
 }
473
-#endif
474
-
475
-#if !defined(HAVE_STRNLEN) || defined(HAVE_STRNI)
476
-size_t cli_strnlen(const char *s, size_t n)
477
-{
478
-    size_t i = 0;
479
-    for (; (i < n) && s[i] != '\0'; ++i)
480
-        ;
481
-    return i;
482
-}
483
-#endif
484 472
 
485
-#if !defined(HAVE_STRNDUP) || defined(HAVE_STRNI)
486
-char *cli_strndup(const char *s, size_t n)
473
+char *__cli_strndup(const char *s, size_t n)
487 474
 {
488 475
     char *alloc;
489 476
     size_t len;
... ...
@@ -492,7 +479,7 @@ char *cli_strndup(const char *s, size_t n)
492 492
         return NULL;
493 493
     }
494 494
 
495
-    len   = cli_strnlen(s, n);
495
+    len   = CLI_STRNLEN(s, n);
496 496
     alloc = malloc(len + 1);
497 497
 
498 498
     if (!alloc) {
... ...
@@ -503,9 +490,15 @@ char *cli_strndup(const char *s, size_t n)
503 503
     alloc[len] = '\0';
504 504
     return alloc;
505 505
 }
506
-#endif
507 506
 
508
-#if !defined(HAVE_STRNSTR) || defined(HAVE_STRNI)
507
+size_t __cli_strnlen(const char *s, size_t n)
508
+{
509
+    size_t i = 0;
510
+    for (; (i < n) && s[i] != '\0'; ++i)
511
+        ;
512
+    return i;
513
+}
514
+
509 515
 /*
510 516
  * @brief Find the first occurrence of find in s.
511 517
  *
... ...
@@ -526,7 +519,7 @@ char *cli_strndup(const char *s, size_t n)
526 526
  * @param slen   haystack length
527 527
  * @return char* Address of the needle, if found, else NULL.
528 528
  */
529
-char *cli_strnstr(const char *s, const char *find, size_t slen)
529
+char *__cli_strnstr(const char *s, const char *find, size_t slen)
530 530
 {
531 531
     char c, sc;
532 532
     size_t len;
... ...
@@ -545,7 +538,6 @@ char *cli_strnstr(const char *s, const char *find, size_t slen)
545 545
     }
546 546
     return ((char *)s);
547 547
 }
548
-#endif
549 548
 
550 549
 size_t cli_strtokenize(char *buffer, const char delim, const size_t token_count,
551 550
                        const char **tokens)
... ...
@@ -1214,13 +1206,13 @@ cl_error_t cli_basename(const char *filepath, size_t filepath_len,
1214 1214
     if ((index != filepath) || (index[0] == PATHSEP[0]))
1215 1215
         index++;
1216 1216
 
1217
-    if (0 == cli_strnlen(index, filepath_len - (index - filepath))) {
1217
+    if (0 == CLI_STRNLEN(index, filepath_len - (index - filepath))) {
1218 1218
         cli_dbgmsg("cli_basename: Provided path does not include a file name.\n");
1219 1219
         status = CL_EFORMAT;
1220 1220
         goto done;
1221 1221
     }
1222 1222
 
1223
-    *filebase = cli_strndup(index, filepath_len - (index - filepath));
1223
+    *filebase = CLI_STRNDUP(index, filepath_len - (index - filepath));
1224 1224
     if (NULL == *filebase) {
1225 1225
         cli_errmsg("cli_basename: Failed to allocate memory for file basename.\n");
1226 1226
         status = CL_EMEM;
... ...
@@ -30,38 +30,44 @@
30 30
 #include <limits.h>
31 31
 
32 32
 #include "clamav.h"
33
-#include "clamav.h"
34 33
 
35 34
 #define SIZE_T_CHARLEN ((sizeof(size_t) * CHAR_BIT + 2) / 3 + 1)
36 35
 
37 36
 #ifdef HAVE_STRCASESTR
38
-#define cli_strcasestr strcasestr
37
+#define CLI_STRCASESTR strcasestr
39 38
 #else
40
-const char *cli_strcasestr(const char *haystack, const char *needle);
39
+#define CLI_STRCASESTR __cli_strcasestr
41 40
 #endif
42 41
 
43 42
 #if defined(HAVE_STRNDUP) && !defined(HAVE_STRNI)
44
-#define cli_strndup strndup
43
+#define CLI_STRNDUP strndup
45 44
 #else
46
-char *cli_strndup(const char *s, size_t n);
45
+#define CLI_STRNDUP __cli_strndup
47 46
 #endif
48 47
 
49 48
 #if defined(HAVE_STRNLEN) && !defined(HAVE_STRNI)
50
-#define cli_strnlen strnlen
49
+#define CLI_STRNLEN strnlen
51 50
 #else
52
-size_t cli_strnlen(const char *s, size_t n);
51
+#define CLI_STRNLEN __cli_strnlen
53 52
 #endif
54 53
 
55 54
 #if defined(HAVE_STRNSTR) && !defined(HAVE_STRNI)
56
-#define cli_strnstr strnstr
55
+#define CLI_STRNSTR strnstr
57 56
 #else
58
-char *cli_strnstr(const char *s, const char *find, size_t slen);
57
+#define CLI_STRNSTR __cli_strnstr
59 58
 #endif
60 59
 
61 60
 #include <stdio.h>
62
-#define cli_nocase(val) tolower(val)
63
-#define cli_nocasei(val) toupper(val)
61
+#define CLI_NOCASE(val) tolower(val)
62
+#define CLI_NOCASEI(val) toupper(val)
63
+
64
+/* Custom implementations for systems that do not include these functions. */
65
+const char *__cli_strcasestr(const char *haystack, const char *needle);
66
+char *__cli_strndup(const char *s, size_t n);
67
+size_t __cli_strnlen(const char *s, size_t n);
68
+char *__cli_strnstr(const char *s, const char *find, size_t slen);
64 69
 
70
+/* Custom string-manipulation functions */
65 71
 int cli_strbcasestr(const char *haystack, const char *needle);
66 72
 int cli_chomp(char *string);
67 73
 char *cli_strtok(const char *line, int field, const char *delim);
... ...
@@ -182,6 +182,10 @@ EXPORTS cli_genhash_pe @44384 NONAME
182 182
 EXPORTS cli_free_vba_project @44385 NONAME
183 183
 EXPORTS cli_strdup @44386 NONAME
184 184
 EXPORTS cli_realloc @44387 NONAME
185
+EXPORTS __cli_strcasestr @44388 NONAME
186
+EXPORTS __cli_strndup @44389 NONAME
187
+EXPORTS __cli_strnlen @44390 NONAME
188
+EXPORTS __cli_strnstr @44391 NONAME
185 189
 
186 190
 ; compatibility layer, tommath, zlib
187 191
 EXPORTS w32_srand @44269 NONAME