examples: Remove all uses of strcpy in examples (except for validchr.c).
authorJeremy Allison <jra@samba.org>
Wed, 16 Mar 2016 22:09:12 +0000 (15:09 -0700)
committerMartin Schwenke <martins@samba.org>
Tue, 22 Mar 2016 03:38:24 +0000 (04:38 +0100)
I can't figure out how to make git handle the CR/LF differences
in this file.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Martin Schwenke <martin@meltin.net>
examples/libsmbclient/testacl.c
examples/libsmbclient/testbrowse2.c
examples/libsmbclient/testsmbc.c
examples/libsmbclient/testtruncate.c
examples/libsmbclient/testwrite.c

index 99a6d136e2c845aec18c838efb9cc00f842261fe..055e38cc70f410b40feba90016fb3ef4013cee8e 100644 (file)
@@ -139,7 +139,8 @@ int main(int argc, const char *argv[])
         return 1;
     }
     
-    strcpy(path, poptGetArg(pc));
+    strncpy(path, poptGetArg(pc), sizeof(path));
+    path[sizeof(path)-1] = '\0';
     
     if (smbc_init(get_auth_data_fn, debug) != 0)
     {
index 123660f1b7d7b5d392366f297470258de21e1517..ac2063d613d3f0c3d947be78324c6b090a2ce7dc 100644 (file)
@@ -68,16 +68,18 @@ static smbitem* get_smbitem_list(SMBCCTX *ctx, char *smb_path){
     if ((fd = smbc_getFunctionOpendir(ctx)(ctx, smb_path)) == NULL)
         return NULL;
     while((dirent = smbc_getFunctionReaddir(ctx)(ctx, fd)) != NULL){
+       size_t slen;
        if (strcmp(dirent->name, "") == 0) continue;
        if (strcmp(dirent->name, ".") == 0) continue;
        if (strcmp(dirent->name, "..") == 0) continue;
        
-       if ((item = malloc(sizeof(smbitem) + strlen(dirent->name))) == NULL)
+       slen = strlen(dirent->name)+1;
+       if ((item = malloc(sizeof(smbitem) + slen)) == NULL)
            continue;
        
        item->next = list;
        item->type = dirent->smbc_type;
-       strcpy(item->name, dirent->name);
+       memcpy(item->name, dirent->name, slen);
        list = item;
     }
     smbc_getFunctionClose(ctx)(ctx, fd);
@@ -113,7 +115,8 @@ static void recurse(SMBCCTX *ctx, const char *smb_group, char *smb_path, int max
                else print_smb_path(smb_group, list->name);
                
                if (maxlen < 7 + strlen(list->name)) break;
-               strcpy(smb_path + 6, list->name);
+               strncpy(smb_path + 6, list->name, maxlen - 6);
+               smb_path[maxlen-1] = '\0';
                if ((ctx1 = create_smbctx()) != NULL){
                    recurse(ctx1, smb_group, smb_path, maxlen);
                    delete_smbctx(ctx1);
@@ -128,7 +131,8 @@ static void recurse(SMBCCTX *ctx, const char *smb_group, char *smb_path, int max
                if (maxlen < len + strlen(list->name) + 2) break;
                
                smb_path[len] = '/';
-               strcpy(smb_path + len + 1, list->name);
+               strncpy(smb_path + len + 1, list->name, maxlen - len - 1);
+               smb_path[maxlen-1] = '\0';
                print_smb_path(smb_group, smb_path + 6);
                if (list->type != SMBC_FILE){
                    recurse(ctx, smb_group, smb_path, maxlen);
index 1f98c3afa7a0a454494b6bef04cb43d52b49d156..3c9aa5674cdaab40cd988a72abadcb6c081952b3 100644 (file)
@@ -115,7 +115,7 @@ int main(int argc, char *argv[])
   /* Now, write some date to the file ... */
 
   memset(buff, '\0', sizeof(buff));
-  strcpy(buff, "Some test data for the moment ...");
+  snprintf(buff, sizeof(buff), "%s", "Some test data for the moment ...");
 
   err = smbc_write(fd, buff, sizeof(buff));
 
index 3e29ad225c718e324da46d93b2333f31f35ea84f..1b4298db26ceae68f311d666a513c65df8c55af9 100644 (file)
@@ -32,7 +32,7 @@ int main(int argc, char * argv[])
         return 1;
     }
 
-    strcpy(buffer, "Hello world.\nThis is a test.\n");
+    snprintf(buffer, sizeof(buffer), "%s", "Hello world.\nThis is a test.\n");
 
     ret = smbc_write(fd, buffer, strlen(buffer));
     savedErrno = errno;
index b641a08a1ce07164514b43e04f422f4467bf13cf..636cb2046de126f66c5768de937994c110335655 100644 (file)
@@ -47,7 +47,7 @@ int main(int argc, char * argv[])
             continue;
         }
 
-        strcpy(buffer, "Hello world\n");
+        snprintf(buffer, sizeof(buffer), "%s", "Hello world\n");
 
         ret = smbc_write(fd, buffer, strlen(buffer));
         savedErrno = errno;