Browse code

m4 - rework of strndup and strnlen function absence handling

Mickey Sola authored on 2017/08/17 06:03:39
Showing 8 changed files
... ...
@@ -25,37 +25,6 @@
25 25
 
26 26
 #include <stdio.h>
27 27
 #include <string.h>
28
-#if defined(C_SOLARIS)
29
-size_t strnlen(const char *s, size_t n) __attribute__((weak));
30
-size_t strnlen(const char *s, size_t n)
31
-{
32
-    size_t i = 0;
33
-    for(; (i < n) && s[i] != '\0'; ++i);
34
-    return i;
35
-}
36
-
37
-char *strndup(const char *s, size_t n) __attribute__((weak));
38
-char *strndup(const char *s, size_t n)
39
-{
40
-    char *alloc;
41
-    size_t len;
42
-
43
-    if(!s) {
44
-        return NULL;
45
-    }
46
-
47
-    len = strnlen(s, n);
48
-    alloc = malloc(len+1);
49
-
50
-    if(!alloc) {
51
-        return NULL;
52
-    } else
53
-        memcpy(alloc, s, len);
54
-
55
-    alloc[len] = '\0';
56
-    return alloc;
57
-}
58
-#endif
59 28
 #include <sys/types.h>
60 29
 #ifndef	_WIN32
61 30
 #include <sys/socket.h>
... ...
@@ -68,6 +37,7 @@ char *strndup(const char *s, size_t n)
68 68
 #endif
69 69
 
70 70
 #include "libclamav/clamav.h"
71
+#include "libclamav/str.h"
71 72
 
72 73
 #include "shared/optparser.h"
73 74
 #include "shared/output.h"
... ...
@@ -142,7 +112,7 @@ int localserver(const struct optstruct *opts)
142 142
         pos = server.sun_path + strlen(server.sun_path);
143 143
         while (pos != server.sun_path) {
144 144
             if (*pos == '/') {
145
-                sockdir = strndup(server.sun_path, strlen(server.sun_path) - cnt);
145
+                sockdir = cli_strndup(server.sun_path, strlen(server.sun_path) - cnt);
146 146
                 break;
147 147
             }
148 148
             else {
... ...
@@ -43,6 +43,7 @@
43 43
 
44 44
 #include "libclamav/clamav.h"
45 45
 #include "libclamav/scanners.h"
46
+#include "libclamav/str.h"
46 47
 
47 48
 #include "shared/optparser.h"
48 49
 #include "shared/output.h"
... ...
@@ -438,7 +439,7 @@ static int onas_add_hashnode_child(struct onas_hnode *node, const char* dirname)
438 438
 	if (!child) return CL_EMEM;
439 439
 	
440 440
 	size_t n = strlen(dirname);
441
-	child->dirname = strndup(dirname, n);
441
+	child->dirname = cli_strndup(dirname, n);
442 442
 
443 443
 	onas_add_listnode(node->childtail, child);
444 444
 
... ...
@@ -500,7 +501,7 @@ inline static char *onas_get_parent(const char *pathname, size_t len) {
500 500
 		idx++;
501 501
 	}
502 502
 
503
-	ret = strndup(pathname, idx);
503
+	ret = cli_strndup(pathname, idx);
504 504
 	if (!ret) {
505 505
 		errno = ENOMEM;
506 506
 		return NULL;
... ...
@@ -596,7 +597,7 @@ int onas_ht_add_hierarchy(struct onas_ht *ht, const char *pathname) {
596 596
 				if (!hnode) return CL_EMEM;
597 597
 
598 598
 				hnode->pathlen = curr->fts_pathlen;
599
-				hnode->pathname = strndup(curr->fts_path, hnode->pathlen);
599
+				hnode->pathname = cli_strndup(curr->fts_path, hnode->pathlen);
600 600
 
601 601
 				hnode->prnt_pathname = onas_get_parent(hnode->pathname, hnode->pathlen);
602 602
 				if (hnode->prnt_pathname)
... ...
@@ -365,16 +365,6 @@ static const unsigned int fragsz[] = {
365 365
 };
366 366
 #endif
367 367
 
368
-#if defined(C_SOLARIS)
369
-size_t strnlen(const char *s, size_t n) __attribute__((weak));
370
-size_t strnlen(const char *s, size_t n)
371
-{
372
-    size_t i = 0;
373
-    for(; (i < n) && s[i] != '\0'; ++i);
374
-    return i;
375
-}
376
-#endif
377
-
378 368
 #define FRAGSBITS (sizeof(fragsz)/sizeof(fragsz[0]))
379 369
 
380 370
 struct MPMAP {
... ...
@@ -805,7 +795,7 @@ char *cli_mpool_strndup(mpool_t *mp, const char *s, size_t n) {
805 805
     return NULL;
806 806
   }
807 807
 
808
-  strsz = strnlen(s, n) + 1;
808
+  strsz = cli_strnlen(s, n) + 1;
809 809
   alloc = mpool_malloc(mp, strsz);
810 810
   if(!alloc)
811 811
     cli_errmsg("cli_mpool_strndup(): Can't allocate memory (%lu bytes).\n", (unsigned long) strsz);
... ...
@@ -725,7 +725,6 @@ void *cli_calloc(size_t nmemb, size_t size);
725 725
 void *cli_realloc(void *ptr, size_t size);
726 726
 void *cli_realloc2(void *ptr, size_t size);
727 727
 char *cli_strdup(const char *s);
728
-char *cli_strndup(const char *s, size_t n);
729 728
 int cli_rmdirs(const char *dirname);
730 729
 char *cli_hashstream(FILE *fs, unsigned char *digcpy, int type);
731 730
 char *cli_hashfile(const char *filename, int type);
... ...
@@ -118,16 +118,6 @@ void cli_logg_unsetup(void)
118 118
 }
119 119
 #endif
120 120
 
121
-#if defined(C_SOLARIS)
122
-size_t strnlen(const char *s, size_t n) __attribute__((weak));
123
-size_t strnlen(const char *s, size_t n)
124
-{
125
-    size_t i = 0;
126
-    for(; (i < n) && s[i] != '\0'; ++i);
127
-    return i;
128
-}
129
-#endif
130
-
131 121
 uint8_t cli_debug_flag = 0;
132 122
 uint8_t cli_always_gen_section_hash = 0;
133 123
 
... ...
@@ -295,30 +285,6 @@ char *cli_strdup(const char *s)
295 295
     return alloc;
296 296
 }
297 297
 
298
-char *cli_strndup(const char *s, size_t n)
299
-{
300
-        char *alloc;
301
-        size_t len;
302
-
303
-    if(s == NULL) {
304
-        cli_errmsg("cli_strndup(): s == NULL. Please report to http://bugs.clamav.net\n");
305
-        return NULL;
306
-    }
307
-
308
-    len = strnlen(s, n);
309
-    alloc = malloc(len+1);
310
-
311
-    if(!alloc) {
312
-        perror("strndup_problem");
313
-        cli_errmsg("cli_strndup(): Can't allocate memory (%u bytes).\n", (unsigned int) strlen(s));
314
-        return NULL;
315
-    } else
316
-        memcpy(alloc, s, len);
317
-
318
-    alloc[len] = '\0';
319
-    return alloc;
320
-}
321
-
322 298
 /* returns converted timestamp, in case of error the returned string contains at least one character */
323 299
 const char* cli_ctime(const time_t *timep, char *buf, const size_t bufsize)
324 300
 {
... ...
@@ -460,6 +460,38 @@ const char* cli_strcasestr(const char* a, const char *b)
460 460
 }
461 461
 #endif
462 462
 
463
+#ifndef HAVE_STRNLEN
464
+size_t cli_strnlen(const char *s, size_t n)
465
+{
466
+    size_t i = 0;
467
+    for(; (i < n) && s[i] != '\0'; ++i);
468
+    return i;
469
+}
470
+#endif
471
+
472
+#ifndef HAVE_STRNDUP
473
+char *cli_strndup(const char *s, size_t n)
474
+{
475
+    char *alloc;
476
+    size_t len;
477
+
478
+    if(!s) {
479
+        return NULL;
480
+    }
481
+
482
+    len = strnlen(s, n);
483
+    alloc = malloc(len+1);
484
+
485
+    if(!alloc) {
486
+        return NULL;
487
+    } else
488
+        memcpy(alloc, s, len);
489
+
490
+    alloc[len] = '\0';
491
+    return alloc;
492
+}
493
+#endif
494
+
463 495
 size_t cli_strtokenize(char *buffer, const char delim, const size_t token_count, const char **tokens)
464 496
 {
465 497
 	size_t tokens_found, i;
... ...
@@ -33,6 +33,18 @@
33 33
 const char *cli_strcasestr(const char *haystack, const char *needle);
34 34
 #endif
35 35
 
36
+#ifdef HAVE_STRNDUP
37
+#define cli_strndup strndup
38
+#else
39
+char *cli_strndup(const char *s, size_t n);
40
+#endif
41
+
42
+#ifdef HAVE_STRNLEN
43
+#define cli_strnlen strnlen
44
+#else
45
+size_t cli_strnlen(const char *s, size_t n);
46
+#endif
47
+
36 48
 #include <stdio.h>
37 49
 #define cli_nocase(val) tolower(val)
38 50
 #define cli_nocasei(val) toupper(val)
... ...
@@ -2,7 +2,9 @@ AX_CHECK_UNAME_SYSCALL
2 2
 AC_CHECK_LIB([socket], [bind], [LIBS="$LIBS -lsocket"; CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lsocket"; FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lsocket"; CLAMD_LIBS="$CLAMD_LIBS -lsocket"])
3 3
 AC_SEARCH_LIBS([gethostent],[nsl], [(LIBS="$LIBS -lnsl"; CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lnsl"; FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lnsl"; CLAMD_LIBS="$CLAMD_LIBS -lnsl")])
4 4
 
5
-AC_CHECK_FUNCS([poll setsid memcpy snprintf vsnprintf strerror_r strlcpy strlcat strcasestr inet_ntop setgroups initgroups ctime_r mkstemp mallinfo madvise getnameinfo])
5
+AC_CHECK_FUNCS_ONCE([poll setsid memcpy snprintf vsnprintf strerror_r strlcpy strlcat strcasestr inet_ntop setgroups initgroups ctime_r mkstemp mallinfo madvise getnameinfo])
6
+AC_CHECK_FUNCS([strndup])
7
+AC_CHECK_FUNCS([strnlen])
6 8
 AC_FUNC_FSEEKO
7 9
 
8 10
 dnl Check if anon maps are available, check if we can determine the page size