From 4609058a9c35cf76008f3ec65da3504943cb4a33 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 16 Mar 2016 15:09:12 -0700 Subject: [PATCH] examples: Remove all uses of strcpy in examples (except for validchr.c). I can't figure out how to make git handle the CR/LF differences in this file. Signed-off-by: Jeremy Allison Reviewed-by: Martin Schwenke --- examples/libsmbclient/testacl.c | 3 ++- examples/libsmbclient/testbrowse2.c | 12 ++++++++---- examples/libsmbclient/testsmbc.c | 2 +- examples/libsmbclient/testtruncate.c | 2 +- examples/libsmbclient/testwrite.c | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/libsmbclient/testacl.c b/examples/libsmbclient/testacl.c index 99a6d136e2c..055e38cc70f 100644 --- a/examples/libsmbclient/testacl.c +++ b/examples/libsmbclient/testacl.c @@ -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) { diff --git a/examples/libsmbclient/testbrowse2.c b/examples/libsmbclient/testbrowse2.c index 123660f1b7d..ac2063d613d 100644 --- a/examples/libsmbclient/testbrowse2.c +++ b/examples/libsmbclient/testbrowse2.c @@ -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); diff --git a/examples/libsmbclient/testsmbc.c b/examples/libsmbclient/testsmbc.c index 1f98c3afa7a..3c9aa5674cd 100644 --- a/examples/libsmbclient/testsmbc.c +++ b/examples/libsmbclient/testsmbc.c @@ -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)); diff --git a/examples/libsmbclient/testtruncate.c b/examples/libsmbclient/testtruncate.c index 3e29ad225c7..1b4298db26c 100644 --- a/examples/libsmbclient/testtruncate.c +++ b/examples/libsmbclient/testtruncate.c @@ -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; diff --git a/examples/libsmbclient/testwrite.c b/examples/libsmbclient/testwrite.c index b641a08a1ce..636cb2046de 100644 --- a/examples/libsmbclient/testwrite.c +++ b/examples/libsmbclient/testwrite.c @@ -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; -- 2.34.1