From: Kern Sibbald Date: Tue, 12 May 2020 15:27:58 +0000 (+0200) Subject: Fix new compiler warnings + always use bstrncpy not strncpy to ensure EOS at end... X-Git-Tag: Release-9.6.4~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af9bad0789d23c3f17bf8001711846aa6a2aae5b;p=thirdparty%2Fbacula.git Fix new compiler warnings + always use bstrncpy not strncpy to ensure EOS at end of string --- diff --git a/bacula/src/baconfig.h b/bacula/src/baconfig.h index 939406149..ee47292a4 100644 --- a/bacula/src/baconfig.h +++ b/bacula/src/baconfig.h @@ -546,6 +546,8 @@ int m_msg(const char *file, int line, POOLMEM **msgbuf, const char *fmt,...); int m_msg(const char *file, int line, POOLMEM *&pool_buf, const char *fmt, ...); void t_msg(const char *file, int line, int64_t level, const char *fmt,...); +/* Use bstrncpy instead of strncpy because it ensures last char is a 0 */ +#define strncpy bad_call_on_strncpy_use_bstrncpy /** Use our strdup with smartalloc */ #ifndef HAVE_WXCONSOLE diff --git a/bacula/src/cats/bvfs.c b/bacula/src/cats/bvfs.c index e5c21835f..b3269423d 100644 --- a/bacula/src/cats/bvfs.c +++ b/bacula/src/cats/bvfs.c @@ -342,7 +342,7 @@ static void build_path_hierarchy(JCR *jcr, BDB *mdb, char pathid[50]; ATTR_DBR parent; char *bkp = mdb->path; - strncpy(pathid, org_pathid, sizeof(pathid)); + bstrncpy(pathid, org_pathid, sizeof(pathid)); /* Does the ppathid exist for this ? we use a memory cache... In order to * avoid the full loop, we consider that if a dir is allready in the diff --git a/bacula/src/console/console.c b/bacula/src/console/console.c index 87853615a..365a8c0d0 100644 --- a/bacula/src/console/console.c +++ b/bacula/src/console/console.c @@ -488,9 +488,7 @@ static ItemList *items = NULL; void init_items() { if (!items) { - items = (ItemList*) malloc(sizeof(ItemList)); - memset(items, 0, sizeof(ItemList)); - + items = (ItemList*)bmalloc(sizeof(ItemList)); /* bmalloc clears memory */ } else { items->list.destroy(); } @@ -1259,7 +1257,7 @@ int main(int argc, char *argv[]) exit(0); } - memset(&jcr, 0, sizeof(jcr)); + memset((void *)&jcr, 0, sizeof(jcr)); (void)WSA_Init(); /* Initialize Windows sockets */ diff --git a/bacula/src/dird/dird.c b/bacula/src/dird/dird.c index fdb1d97bf..11c4406ea 100644 --- a/bacula/src/dird/dird.c +++ b/bacula/src/dird/dird.c @@ -1265,7 +1265,7 @@ static bool check_catalog(cat_op mode) /* To copy dbdriver field into "CAT" catalog resource class (local) * from dbdriver in "BDB" catalog DB Interface class (global) */ - strncpy(catalog->db_driver, BDB_db_driver, db_driver_len); + bstrncpy(catalog->db_driver, BDB_db_driver, db_driver_len); } } diff --git a/bacula/src/dird/inc_conf.c b/bacula/src/dird/inc_conf.c index b8d424159..6cdce90bc 100644 --- a/bacula/src/dird/inc_conf.c +++ b/bacula/src/dird/inc_conf.c @@ -387,7 +387,7 @@ static void store_newinc(LEX *lc, RES_ITEM *item, int index, int pass) } if (pass == 1) { incexe = (INCEXE *)malloc(sizeof(INCEXE)); - memcpy(incexe, &res_incexe, sizeof(INCEXE)); + memcpy((void *)incexe, (void *)&res_incexe, sizeof(INCEXE)); bmemset(&res_incexe, 0, sizeof(INCEXE)); if (item->code == 0) { /* include */ if (res_all.res_fs.num_includes == 0) { diff --git a/bacula/src/dird/restore.c b/bacula/src/dird/restore.c index a09daa432..c9a0f1278 100644 --- a/bacula/src/dird/restore.c +++ b/bacula/src/dird/restore.c @@ -147,7 +147,7 @@ static bool open_bootstrap_file(JCR *jcr, bootstrap_info &info) if (!jcr->RestoreBootstrap) { return false; } - strncpy(info.storage, jcr->rstore->name(), MAX_NAME_LENGTH); + bstrncpy(info.storage, jcr->rstore->name(), MAX_NAME_LENGTH); bs = bfopen(jcr->RestoreBootstrap, "rb"); if (!bs) { @@ -166,7 +166,7 @@ static bool open_bootstrap_file(JCR *jcr, bootstrap_info &info) continue; } if (!strcasecmp(ua->argk[0], "Storage")) { - strncpy(info.storage, ua->argv[0], MAX_NAME_LENGTH); + bstrncpy(info.storage, ua->argv[0], MAX_NAME_LENGTH); break; } } @@ -236,7 +236,7 @@ static bool check_for_new_storage(JCR *jcr, bootstrap_info &info) return false; } /* note the next storage name */ - strncpy(info.storage, ua->argv[0], MAX_NAME_LENGTH); + bstrncpy(info.storage, ua->argv[0], MAX_NAME_LENGTH); Dmsg1(5, "Change storage to %s\n", info.storage); return true; } diff --git a/bacula/src/dird/ua_cmds.c b/bacula/src/dird/ua_cmds.c index ceca225ee..e5b185549 100644 --- a/bacula/src/dird/ua_cmds.c +++ b/bacula/src/dird/ua_cmds.c @@ -1994,8 +1994,8 @@ static int cloud_list_cmd(UAContext *ua, const char *cmd) bool first=true; uint32_t maxpart=0, part; uint64_t maxpart_size=0; - memset(&pr, 0, sizeof(pr)); - memset(&mr, 0, sizeof(mr)); + memset((void *)&pr, 0, sizeof(pr)); + memset((void *)&mr, 0, sizeof(mr)); /* Look at arguments */ for (int i=1; iargc; i++) { diff --git a/bacula/src/dird/ua_label.c b/bacula/src/dird/ua_label.c index 9fec5ed19..e792b9907 100644 --- a/bacula/src/dird/ua_label.c +++ b/bacula/src/dird/ua_label.c @@ -1246,7 +1246,7 @@ void status_slots(UAContext *ua, STORE *store_r) } ua->send_msg(_("+------+----------------------+-----------+-----------------+--------------------+\n")); - memset(&mr, 0, sizeof(MEDIA_DBR)); + memset((void *)&mr, 0, sizeof(MEDIA_DBR)); bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName)); if (mr.VolumeName[0] && db_get_media_record(ua->jcr, ua->db, &mr)) { diff --git a/bacula/src/dird/ua_prune.c b/bacula/src/dird/ua_prune.c index 93be8e809..e6d98d375 100644 --- a/bacula/src/dird/ua_prune.c +++ b/bacula/src/dird/ua_prune.c @@ -808,7 +808,7 @@ static bool prune_expired_volumes(UAContext *ua) foreach_alist(val, lst) { nb++; - memset(&mr, 0, sizeof(mr)); + memset((void *)&mr, 0, sizeof(mr)); bstrncpy(mr.VolumeName, val, sizeof(mr.VolumeName)); db_get_media_record(ua->jcr, ua->db, &mr); Mmsg(query, _("Volume \"%s\""), val); diff --git a/bacula/src/dird/ua_run.c b/bacula/src/dird/ua_run.c index 1cc48c271..cf05a1f21 100644 --- a/bacula/src/dird/ua_run.c +++ b/bacula/src/dird/ua_run.c @@ -966,8 +966,7 @@ configure_again: } else if (h == ini_store_name) { found = ini->items[i].found = get_cmd(ua, prompt.c_str()); if (found) { - strncpy(ini->items[i].val.nameval, ua->cmd, MAX_NAME_LENGTH -1); - ini->items[i].val.nameval[MAX_NAME_LENGTH - 1] = 0; + bstrncpy(ini->items[i].val.nameval, ua->cmd, MAX_NAME_LENGTH); } } else if (h == ini_store_str) { diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index eb8fd96b9..dd22fa7d0 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -1180,8 +1180,7 @@ static bool init_fileset(JCR *jcr) if (ff->fileset) { return false; } - fileset = (findFILESET *)malloc(sizeof(findFILESET)); - memset(fileset, 0, sizeof(findFILESET)); + fileset = (findFILESET *)bmalloc(sizeof(findFILESET)); ff->fileset = fileset; fileset->state = state_none; fileset->include_list.init(1, true); @@ -1307,8 +1306,7 @@ findINCEXE *new_exclude(JCR *jcr) findFILESET *fileset = jcr->ff->fileset; /* New exclude */ - fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE)); - memset(fileset->incexe, 0, sizeof(findINCEXE)); + fileset->incexe = (findINCEXE *)bmalloc(sizeof(findINCEXE)); fileset->incexe->opts_list.init(1, true); fileset->incexe->name_list.init(); fileset->incexe->plugin_list.init(); @@ -1324,8 +1322,7 @@ findINCEXE *new_include(JCR *jcr) findFILESET *fileset = jcr->ff->fileset; /* New include */ - fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE)); - memset(fileset->incexe, 0, sizeof(findINCEXE)); + fileset->incexe = (findINCEXE *)bmalloc(sizeof(findINCEXE)); fileset->incexe->opts_list.init(1, true); fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */ fileset->incexe->plugin_list.init(); @@ -1343,8 +1340,7 @@ findINCEXE *new_preinclude(JCR *jcr) findFILESET *fileset = jcr->ff->fileset; /* New pre-include */ - fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE)); - memset(fileset->incexe, 0, sizeof(findINCEXE)); + fileset->incexe = (findINCEXE *)bmalloc(sizeof(findINCEXE)); fileset->incexe->opts_list.init(1, true); fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */ fileset->incexe->plugin_list.init(); @@ -1359,8 +1355,7 @@ static findFOPTS *start_options(FF_PKT *ff) if (state != state_options) { ff->fileset->state = state_options; - findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS)); - memset(fo, 0, sizeof(findFOPTS)); + findFOPTS *fo = (findFOPTS *)bmalloc(sizeof(findFOPTS)); fo->regex.init(1, true); fo->regexdir.init(1, true); fo->regexfile.init(1, true); @@ -1385,8 +1380,7 @@ void new_options(JCR *jcr, findINCEXE *incexe) if (!incexe) { incexe = jcr->ff->fileset->incexe; } - findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS)); - memset(fo, 0, sizeof(findFOPTS)); + findFOPTS *fo = (findFOPTS *)bmalloc(sizeof(findFOPTS)); fo->regex.init(1, true); fo->regexdir.init(1, true); fo->regexfile.init(1, true); diff --git a/bacula/src/filed/restore.c b/bacula/src/filed/restore.c index 9ed0ff2d2..67950a7bc 100644 --- a/bacula/src/filed/restore.c +++ b/bacula/src/filed/restore.c @@ -366,7 +366,7 @@ void do_restore(JCR *jcr) /* ***FIXME*** make configurable */ crypto_digest_t signing_algorithm = have_sha2 ? CRYPTO_DIGEST_SHA256 : CRYPTO_DIGEST_SHA1; - memset(&rctx, 0, sizeof(rctx)); + memset((void *)&rctx, 0, sizeof(rctx)); rctx.jcr = jcr; /* The following variables keep track of "known unknowns" */ diff --git a/bacula/src/findlib/bfile.c b/bacula/src/findlib/bfile.c index da4248327..59d393051 100644 --- a/bacula/src/findlib/bfile.c +++ b/bacula/src/findlib/bfile.c @@ -290,7 +290,7 @@ extern "C" HANDLE get_osfhandle(int fd); void binit(BFILE *bfd) { - memset(bfd, 0, sizeof(BFILE)); + memset((void *)bfd, 0, sizeof(BFILE)); bfd->fid = -1; bfd->mode = BF_CLOSED; bfd->use_backup_api = have_win32_api(); @@ -880,7 +880,7 @@ boffset_t blseek(BFILE *bfd, boffset_t offset, int whence) /* Unix */ void binit(BFILE *bfd) { - memset(bfd, 0, sizeof(BFILE)); + memset((void *)bfd, 0, sizeof(BFILE)); bfd->fid = -1; } diff --git a/bacula/src/findlib/find.c b/bacula/src/findlib/find.c index 47d781237..2c667051c 100644 --- a/bacula/src/findlib/find.c +++ b/bacula/src/findlib/find.c @@ -51,8 +51,8 @@ FF_PKT *init_find_files() { FF_PKT *ff; + /* bmalloc returns zeroed buffer */ ff = (FF_PKT *)bmalloc(sizeof(FF_PKT)); - memset(ff, 0, sizeof(FF_PKT)); ff->sys_fname = get_pool_memory(PM_FNAME); diff --git a/bacula/src/findlib/find_one.c b/bacula/src/findlib/find_one.c index 764a3e5ce..986047d05 100644 --- a/bacula/src/findlib/find_one.c +++ b/bacula/src/findlib/find_one.c @@ -86,7 +86,7 @@ static inline int LINKHASH(const struct stat &info) static FF_PKT *new_dir_ff_pkt(FF_PKT *ff_pkt) { FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT)); - memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT)); + memcpy((void *)dir_ff_pkt, (void *)ff_pkt, sizeof(FF_PKT)); dir_ff_pkt->fname = bstrdup(ff_pkt->fname); dir_ff_pkt->link = bstrdup(ff_pkt->link); dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME); diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index 917ebaf0f..0122b23e7 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -218,6 +218,9 @@ int bmicrosleep(int32_t sec, int32_t usec) return stat; } +/* allow using strncpy in this file */ +#undef strncpy + /* * Guarantee that the string is properly terminated */ char *bstrncpy(char *dest, const char *src, int maxlen) @@ -1007,7 +1010,7 @@ void stack_trace() function = ret; } else { /* demangling failed, just pretend it's a C function with no args */ - strncpy(function, begin, sz); + bstrncpy(function, begin, sz); strncat(function, "()", sz); function[sz-1] = '\0'; } diff --git a/bacula/src/lib/htable.c b/bacula/src/lib/htable.c index 29f46c34c..6711d2c21 100644 --- a/bacula/src/lib/htable.c +++ b/bacula/src/lib/htable.c @@ -227,7 +227,7 @@ void htable::grow_table() Dmsg1(100, "Grow called old size = %d\n", buckets); /* Setup a bigger table */ htable *big = (htable *)malloc(sizeof(htable)); - memcpy(big, this, sizeof(htable)); /* start with original class data */ + memcpy((void *)big, (void *)this, sizeof(htable)); /* start with original class data */ big->loffset = loffset; big->mask = mask<<1 | 1; big->rshift = rshift - 1; diff --git a/bacula/src/lib/ini.c b/bacula/src/lib/ini.c index f3c188f9d..62e94f11d 100644 --- a/bacula/src/lib/ini.c +++ b/bacula/src/lib/ini.c @@ -532,7 +532,7 @@ bool ini_store_name(LEX *lc, ConfigFile *inifile, ini_items *item) return false; } Dmsg1(dbglevel, "ini_store_name: %s\n", lc->str); - strncpy(item->val.nameval, lc->str, sizeof(item->val.nameval)); + bstrncpy(item->val.nameval, lc->str, sizeof(item->val.nameval)); scan_to_eol(lc); return true; } diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index 6728bde50..f643a240c 100644 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -274,7 +274,7 @@ void my_name_is(int argc, char *argv[], const char *name) #endif if (!respath){ /* no resolved_path available in cargv0, so populate it */ - strncpy(cargv0, argv[0], path_max); + bstrncpy(cargv0, argv[0], path_max); } /* strip trailing filename and save exepath */ for (l=p=cargv0; *p; p++) { @@ -789,7 +789,7 @@ 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 */ - strncpy(buf, p, len); + bstrncpy(buf, p, len); buf[len] = 0; syslog(mode, "%s", buf); p = p2+1; /* skip \n */ diff --git a/bacula/src/lib/util.c b/bacula/src/lib/util.c index 78c78efaf..49bba0033 100644 --- a/bacula/src/lib/util.c +++ b/bacula/src/lib/util.c @@ -214,7 +214,7 @@ char *asciidump(const char *data, int len, char *buf, int capacity) char *b=buf; const unsigned char *p=(const unsigned char *)data; if (!data) { - strncpy(buf, "", capacity); + bstrncpy(buf, "", capacity); return buf; } while (len>0 && capacity>1) { @@ -242,7 +242,7 @@ char *smartdump(const char *data, int len, char *buf, int capacity, bool *is_asc int c=capacity; const unsigned char *p=(const unsigned char *)data; if (!data) { - strncpy(buf, "", capacity); + bstrncpy(buf, "", capacity); return buf; } if (is_ascii != NULL) { diff --git a/bacula/src/plugins/fd/bpipe-fd.c b/bacula/src/plugins/fd/bpipe-fd.c index df3695f2d..3de5b2e2b 100644 --- a/bacula/src/plugins/fd/bpipe-fd.c +++ b/bacula/src/plugins/fd/bpipe-fd.c @@ -620,7 +620,7 @@ static bRC createFile(bpContext *ctx, struct restore_pkt *rp) if (strlen(rp->where) > 512) { printf("Restore target dir too long. Restricting to first 512 bytes.\n"); } - strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 512); + bstrncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 512); ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace; rp->create_status = CF_EXTRACT; return bRC_OK; diff --git a/bacula/src/plugins/fd/test-plugin-fd.c b/bacula/src/plugins/fd/test-plugin-fd.c index 0a6373a2a..aac75d8b5 100644 --- a/bacula/src/plugins/fd/test-plugin-fd.c +++ b/bacula/src/plugins/fd/test-plugin-fd.c @@ -675,7 +675,7 @@ static bRC createFile(bpContext *ctx, struct restore_pkt *rp) if (strlen(rp->where) > 990) { printf("Restore target dir too long. Restricting to first 990 bytes.\n"); } - strncpy(pctx->where, rp->where, sizeof(pctx->where)); + bstrncpy(pctx->where, rp->where, sizeof(pctx->where)); pctx->replace = rp->replace; rp->create_status = CF_CORE; return bRC_OK; diff --git a/bacula/src/stored/status.c b/bacula/src/stored/status.c index acfd460dc..7286b88df 100644 --- a/bacula/src/stored/status.c +++ b/bacula/src/stored/status.c @@ -1065,7 +1065,7 @@ bool qstatus_cmd(JCR *jcr) unbash_spaces(collname); } else if (!strcmp(argk[i], "api_opts") && argv[i]) { - strncpy(sp.api_opts, argv[i], sizeof(sp.api_opts)); + bstrncpy(sp.api_opts, argv[i], sizeof(sp.api_opts)); } } diff --git a/bacula/src/tools/fstype.c b/bacula/src/tools/fstype.c index f8240a7c2..65b4d55eb 100644 --- a/bacula/src/tools/fstype.c +++ b/bacula/src/tools/fstype.c @@ -148,7 +148,7 @@ int main (int argc, char *const *argv) } for (i = 0; i < argc; --argc, ++argv) { FF_PKT ff_pkt; - memset(&ff_pkt, 0, sizeof(ff_pkt)); + memset((void *)&ff_pkt, 0, sizeof(ff_pkt)); ff_pkt.fname = ff_pkt.link = *argv; if (lstat(ff_pkt.fname, &ff_pkt.statp) != 0) { fprintf(stderr, "lstat of %s failed.\n", ff_pkt.fname); diff --git a/bacula/src/tools/testfind.c b/bacula/src/tools/testfind.c index de3f863d4..7cd561cf4 100644 --- a/bacula/src/tools/testfind.c +++ b/bacula/src/tools/testfind.c @@ -378,7 +378,7 @@ static void count_files(FF_PKT *ar) trunc_fname++; } if (fnl > 0) { - strncpy(file, l, fnl); /* copy filename */ + bstrncpy(file, l, fnl); /* copy filename */ file[fnl] = 0; } else { file[0] = ' '; /* blank filename */ @@ -394,7 +394,7 @@ static void count_files(FF_PKT *ar) pnl = 255; trunc_path++; } - strncpy(spath, ar->fname, pnl); + bstrncpy(spath, ar->fname, pnl); spath[pnl] = 0; if (pnl == 0) { spath[0] = ' '; @@ -419,8 +419,7 @@ static bool copy_fileset(FF_PKT *ff, JCR *jcr) findFILESET *fileset; findFOPTS *current_opts; - fileset = (findFILESET *)malloc(sizeof(findFILESET)); - memset(fileset, 0, sizeof(findFILESET)); + fileset = (findFILESET *)bmalloc(sizeof(findFILESET)); ff->fileset = fileset; fileset->state = state_none; @@ -441,8 +440,7 @@ static bool copy_fileset(FF_PKT *ff, JCR *jcr) ie = jcr_fileset->include_items[i]; /* New include */ - fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE)); - memset(fileset->incexe, 0, sizeof(findINCEXE)); + fileset->incexe = (findINCEXE *)bmalloc(sizeof(findINCEXE)); fileset->incexe->opts_list.init(1, true); fileset->incexe->name_list.init(0, 0); fileset->include_list.append(fileset->incexe); @@ -450,8 +448,7 @@ static bool copy_fileset(FF_PKT *ff, JCR *jcr) ie = jcr_fileset->exclude_items[i]; /* New exclude */ - fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE)); - memset(fileset->incexe, 0, sizeof(findINCEXE)); + fileset->incexe = (findINCEXE *)bmalloc(sizeof(findINCEXE)); fileset->incexe->opts_list.init(1, true); fileset->incexe->name_list.init(0, 0); fileset->exclude_list.append(fileset->incexe); @@ -460,8 +457,7 @@ static bool copy_fileset(FF_PKT *ff, JCR *jcr) for (j=0; jnum_opts; j++) { FOPTS *fo = ie->opts_list[j]; - current_opts = (findFOPTS *)malloc(sizeof(findFOPTS)); - memset(current_opts, 0, sizeof(findFOPTS)); + current_opts = (findFOPTS *)bmalloc(sizeof(findFOPTS)); fileset->incexe->current_opts = current_opts; fileset->incexe->opts_list.append(current_opts);