From: Michal Rakowski Date: Tue, 2 Feb 2021 10:00:38 +0000 (+0100) Subject: Review bstrncpy() calls X-Git-Tag: Release-11.3.2~762 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1336c3ac4c804bcd1f5fbf0d9f25f43adb7cae0;p=thirdparty%2Fbacula.git Review bstrncpy() calls --- diff --git a/bacula/src/findlib/drivetype.c b/bacula/src/findlib/drivetype.c index c6d21bfca..23fd3421e 100644 --- a/bacula/src/findlib/drivetype.c +++ b/bacula/src/findlib/drivetype.c @@ -57,9 +57,8 @@ bool drivetype(const char *fname, char *dt, int dtlen) CHAR rootpath[4]; UINT type; - /* Copy Drive Letter, colon, and backslash to rootpath */ - bstrncpy(rootpath, fname, 3); - rootpath[3] = '\0'; + /* Copy Drive Letter, colon, and backslash to rootpath. bstrncpy will null-terminate the string */ + bstrncpy(rootpath, fname, sizeof(rootpath); type = GetDriveType(rootpath); diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index e2326fc50..c7f0385a9 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -1959,6 +1959,10 @@ int main(int argc, char **argv) ok(get_user_home_directory("root", home.addr()) == 0, "get_user_home_directory()"); + const char *p = "/@MYSQL/xxxx"; + char type[10]; + bstrncpy(type, p, 8); + ok(strcmp(type, "/@MYSQL") == 0, "bstrncpy()"); return report(); } #endif diff --git a/bacula/src/lib/events.c b/bacula/src/lib/events.c index 1b0a2b7d9..1698fae40 100644 --- a/bacula/src/lib/events.c +++ b/bacula/src/lib/events.c @@ -176,7 +176,10 @@ int MSGS::add_custom_type(bool is_not, char *type) } int len = strlen(type); - t = (CUSTOM_TYPE*) malloc(sizeof(CUSTOM_TYPE)+len+1); + /* No need for additional byte for null terminating byte since + * placeholder for it is already in CUSTOM_TYPE struct + */ + t = (CUSTOM_TYPE*) malloc(sizeof(CUSTOM_TYPE)+len); bstrncpy(t->kw, type, len+1); CUSTOM_TYPE *t2 = (CUSTOM_TYPE*) custom_type->insert(t, custom_type_insert); if (t2 == t) { diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index 4e4451b1d..8a83b376e 100644 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -833,9 +833,8 @@ static void send_to_syslog(int mode, const char *msg) } while (*p && ((p2 = strchr(p, '\n')) != NULL)) { - len = MIN((int)sizeof(buf) - 1, p2 - p + 1); /* Add 1 to keep \n */ + len = MIN((int)sizeof(buf), p2 - p + 1); /* Add 1 to keep \n */ bstrncpy(buf, p, len); - buf[len] = 0; syslog(mode, "%s", buf); p = p2+1; /* skip \n */ } diff --git a/bacula/src/plugins/fd/bpipe-fd.c b/bacula/src/plugins/fd/bpipe-fd.c index ab363fd16..6d9ad03a5 100644 --- a/bacula/src/plugins/fd/bpipe-fd.c +++ b/bacula/src/plugins/fd/bpipe-fd.c @@ -617,10 +617,11 @@ static bRC endRestoreFile(bpContext *ctx) static bRC createFile(bpContext *ctx, struct restore_pkt *rp) { // printf("bpipe-fd: createFile\n"); - if (strlen(rp->where) > 512) { - printf("Restore target dir too long. Restricting to first 512 bytes.\n"); + uint sz = sizeof(((struct plugin_ctx *)ctx->pContext)->where); + if (strlen(rp->where) > sz) { + printf("Restore target dir too long. Restricting to first %u bytes.\n", sz); } - bstrncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 512); + bstrncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, sz); ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace; rp->create_status = CF_EXTRACT; return bRC_OK; diff --git a/bacula/src/stored/job.c b/bacula/src/stored/job.c index 79a22e86d..739744679 100644 --- a/bacula/src/stored/job.c +++ b/bacula/src/stored/job.c @@ -149,7 +149,7 @@ bool job_cmd(JCR *jcr) * Pass back an authorization key for the File daemon */ if (jcr->sd_client) { - bstrncpy(sd_auth_key, "xxx", 3); + bstrncpy(sd_auth_key, "xxx", 4); /* include \0 */ } else { bsnprintf(seed, sizeof(seed), "%p%d", jcr, JobId); make_session_key(sd_auth_key, seed, 1); diff --git a/bacula/src/win32/filed/plugins/alldrives-fd.c b/bacula/src/win32/filed/plugins/alldrives-fd.c index f93e83b30..e4177e608 100644 --- a/bacula/src/win32/filed/plugins/alldrives-fd.c +++ b/bacula/src/win32/filed/plugins/alldrives-fd.c @@ -257,8 +257,7 @@ static bool drivetype(const char *fname, char *dt, int dtlen) UINT type; /* Copy Drive Letter, colon, and backslash to rootpath */ - bstrncpy(rootpath, fname, 3); - rootpath[3] = '\0'; + bstrncpy(rootpath, fname, 4); type = GetDriveType(rootpath);