]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Review bstrncpy() calls
authorMichal Rakowski <michal.rakowski@baculasystems.com>
Tue, 2 Feb 2021 10:00:38 +0000 (11:00 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:02:59 +0000 (09:02 +0100)
bacula/src/findlib/drivetype.c
bacula/src/lib/bsys.c
bacula/src/lib/events.c
bacula/src/lib/message.c
bacula/src/plugins/fd/bpipe-fd.c
bacula/src/stored/job.c
bacula/src/win32/filed/plugins/alldrives-fd.c

index c6d21bfcabe3f9041dd7f56e95e1e14de59a8f9a..23fd3421ef0bbea453aaac7f92787703490eba6d 100644 (file)
@@ -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);
 
index e2326fc50279b20ff14284fb9aa1f1d194a559e5..c7f0385a98bdf247c770f9df1f4d15ccc91ee890 100644 (file)
@@ -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
index 1b0a2b7d97c178e75ccd198ea691d0c09dc065ed..1698fae40ff45c03d7e7426b07599f3bf0cbece4 100644 (file)
@@ -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) {
index 4e4451b1d6163b46c10c6a67e18ed0ef9b403e79..8a83b376e8aa95b854c62a47bac851e1eb18ea5b 100644 (file)
@@ -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 */
    }
index ab363fd16efeb60f65197dce1d24332fb454fe45..6d9ad03a521db0a15784987105c213344f9f122d 100644 (file)
@@ -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;
index 79a22e86d0f65af59270d7598a00bf0e140bab61..739744679472c7481303ea2a4cc77ad8d58efeca 100644 (file)
@@ -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);
index f93e83b30f61e326071b6a6a59548c96719639ee..e4177e6087ce91da89a7606692d5938dadec595c 100644 (file)
@@ -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);