Browse code

implement cli_strndup and cli_mpool_strndup

Kevin Lin authored on 2016/07/14 04:52:17
Showing 5 changed files
... ...
@@ -785,6 +785,25 @@ char *cli_mpool_strdup(mpool_t *mp, const char *s) {
785 785
   return alloc;
786 786
 }
787 787
 
788
+char *cli_mpool_strndup(mpool_t *mp, const char *s, size_t n) {
789
+  char *alloc;
790
+  size_t strsz;
791
+
792
+  if(s == NULL) {
793
+    cli_errmsg("cli_mpool_strndup(): s == NULL. Please report to http://bugs.clamav.net\n");
794
+    return NULL;
795
+  }
796
+
797
+  strsz = strnlen(s, n) + 1;
798
+  alloc = mpool_malloc(mp, strsz);
799
+  if(!alloc)
800
+    cli_errmsg("cli_mpool_strndup(): Can't allocate memory (%lu bytes).\n", (unsigned long) strsz);
801
+  else
802
+    memcpy(alloc, s, strsz-1);
803
+  alloc[strsz-1] = '\0';
804
+  return alloc;
805
+}
806
+
788 807
 /* #define EXPAND_PUA */
789 808
 char *cli_mpool_virname(mpool_t *mp, const char *virname, unsigned int official) {
790 809
   char *newname, *pt;
... ...
@@ -36,6 +36,7 @@ void *mpool_realloc(mpool_t *mpool, void *ptr, size_t size);
36 36
 void *mpool_realloc2(mpool_t *mpool, void *ptr, size_t size);
37 37
 unsigned char *cli_mpool_hex2str(mpool_t* mpool, const char *src);
38 38
 char *cli_mpool_strdup(mpool_t *mpool, const char *s);
39
+char *cli_mpool_strndup(mpool_t *mpool, const char *s, size_t n);
39 40
 char *cli_mpool_virname(mpool_t *mpool, const char *virname, unsigned int official);
40 41
 uint16_t *cli_mpool_hex2ui(mpool_t *mpool, const char *hex);
41 42
 void mpool_flush(mpool_t *mpool);
... ...
@@ -50,6 +51,7 @@ typedef void mpool_t;
50 50
 #define mpool_realloc2(a, b, c) cli_realloc2(b, c)
51 51
 #define cli_mpool_hex2str(mpool, src) cli_hex2str(src)
52 52
 #define cli_mpool_strdup(mpool, s) cli_strdup(s)
53
+#define cli_mpool_strndup(mpool, s, n) cli_strdup(s, n)
53 54
 #define cli_mpool_virname(mpool, a, b) cli_virname(a, b)
54 55
 #define cli_mpool_hex2ui(mpool, hex) cli_hex2ui(hex)
55 56
 #define mpool_flush(val)
... ...
@@ -694,6 +694,7 @@ void *cli_calloc(size_t nmemb, size_t size);
694 694
 void *cli_realloc(void *ptr, size_t size);
695 695
 void *cli_realloc2(void *ptr, size_t size);
696 696
 char *cli_strdup(const char *s);
697
+char *cli_strndup(const char *s, size_t n);
697 698
 int cli_rmdirs(const char *dirname);
698 699
 char *cli_hashstream(FILE *fs, unsigned char *digcpy, int type);
699 700
 char *cli_hashfile(const char *filename, int type);
... ...
@@ -284,6 +284,30 @@ char *cli_strdup(const char *s)
284 284
     return alloc;
285 285
 }
286 286
 
287
+char *cli_strndup(const char *s, size_t n)
288
+{
289
+        char *alloc;
290
+        size_t len;
291
+
292
+    if(s == NULL) {
293
+        cli_errmsg("cli_strndup(): s == NULL. Please report to http://bugs.clamav.net\n");
294
+        return NULL;
295
+    }
296
+
297
+    len = strnlen(s, n);
298
+    alloc = malloc(len+1);
299
+
300
+    if(!alloc) {
301
+        perror("strndup_problem");
302
+        cli_errmsg("cli_strndup(): Can't allocate memory (%u bytes).\n", (unsigned int) strlen(s));
303
+        return NULL;
304
+    } else
305
+        memcpy(alloc, s, len);
306
+
307
+    alloc[len] = '\0';
308
+    return alloc;
309
+}
310
+
287 311
 /* returns converted timestamp, in case of error the returned string contains at least one character */
288 312
 const char* cli_ctime(const time_t *timep, char *buf, const size_t bufsize)
289 313
 {
... ...
@@ -2307,7 +2307,7 @@ static inline int hash_impfns(cli_ctx *ctx, void **hashctx, uint32_t *impsz, str
2307 2307
                 if (offset >= 0) {
2308 2308
                     /* Hint field is a uint16_t and precedes the Name field */
2309 2309
                     if ((buffer = fmap_need_off_once(map, offset+sizeof(uint16_t), MIN(PE_MAXNAMESIZE, fsize-offset))) != NULL) {
2310
-                        funcname = strndup(buffer, MIN(PE_MAXNAMESIZE, fsize-offset));
2310
+                        funcname = cli_strndup(buffer, MIN(PE_MAXNAMESIZE, fsize-offset));
2311 2311
                         if (funcname == NULL) {
2312 2312
                             cli_dbgmsg("scan_pe: cannot duplicate function name\n");
2313 2313
                             return CL_EMEM;
... ...
@@ -2343,7 +2343,7 @@ static inline int hash_impfns(cli_ctx *ctx, void **hashctx, uint32_t *impsz, str
2343 2343
                 if (offset >= 0) {
2344 2344
                     /* Hint field is a uint16_t and precedes the Name field */
2345 2345
                     if ((buffer = fmap_need_off_once(map, offset+sizeof(uint16_t), MIN(PE_MAXNAMESIZE, fsize-offset))) != NULL) {
2346
-                        funcname = strndup(buffer, MIN(PE_MAXNAMESIZE, fsize-offset));
2346
+                        funcname = cli_strndup(buffer, MIN(PE_MAXNAMESIZE, fsize-offset));
2347 2347
                         if (funcname == NULL) {
2348 2348
                             cli_dbgmsg("scan_pe: cannot duplicate function name\n");
2349 2349
                             return CL_EMEM;
... ...
@@ -2462,7 +2462,7 @@ static unsigned int hash_imptbl(cli_ctx *ctx, unsigned char **digest, uint32_t *
2462 2462
             goto hash_imptbl_end;
2463 2463
         }
2464 2464
 
2465
-        dllname = strndup(buffer, MIN(PE_MAXNAMESIZE, fsize-offset));
2465
+        dllname = cli_strndup(buffer, MIN(PE_MAXNAMESIZE, fsize-offset));
2466 2466
         if (dllname == NULL) {
2467 2467
             cli_dbgmsg("scan_pe: cannot duplicate dll name\n");
2468 2468
             ret = CL_EMEM;