This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=a9d1ee8b0a6f946ce577261a6823bb51598e2bbe commit a9d1ee8b0a6f946ce577261a6823bb51598e2bbe Author: Guillem Jover <guil...@debian.org> AuthorDate: Tue Feb 14 02:44:30 2023 +0100 src: Reduce variables scope Warned-by: cppcheck Changelog: internal --- src/common/force.c | 5 +++-- src/deb/extract.c | 6 +++--- src/deb/info.c | 23 ++++++++++++++++------- src/main/archives.c | 6 ++++-- src/main/depcon.c | 3 ++- src/main/help.c | 6 ++++-- src/main/main.c | 3 ++- src/main/packages.c | 3 ++- src/main/select.c | 6 ++++-- src/main/trigproc.c | 4 ++-- src/main/unpack.c | 8 ++++++-- src/main/verify.c | 5 +++-- src/query/main.c | 32 ++++++++++++++++++++------------ src/split/join.c | 3 ++- t/cppcheck/cppcheck.supp | 3 --- 15 files changed, 73 insertions(+), 43 deletions(-) diff --git a/src/common/force.c b/src/common/force.c index 2e1fd78c9..441010839 100644 --- a/src/common/force.c +++ b/src/common/force.c @@ -283,8 +283,6 @@ print_forceinfo(const struct forceinfo *fi) void parse_force(const char *value, bool set) { - const char *comma; - size_t l; const struct forceinfo *fip; if (strcmp(value, "help") == 0) { @@ -318,6 +316,9 @@ parse_force(const char *value, bool set) } for (;;) { + const char *comma; + size_t l; + comma = strchrnul(value, ','); l = (size_t)(comma - value); for (fip = forceinfos; fip->name; fip++) diff --git a/src/deb/extract.c b/src/deb/extract.c index 73612a454..e643bbc5c 100644 --- a/src/deb/extract.c +++ b/src/deb/extract.c @@ -119,8 +119,6 @@ extracthalf(const char *debar, const char *dir, int p1[2], p2[2]; int p2_out; char nlc; - int adminmember = -1; - bool header_done; struct compress_params decompress_params = { .type = COMPRESSOR_TYPE_GZIP, .threads_max = compress_params.threads_max, @@ -133,8 +131,10 @@ extracthalf(const char *debar, const char *dir, read_fail(r, debar, _("archive magic version number")); if (strcmp(versionbuf, DPKG_AR_MAGIC) == 0) { + int adminmember = -1; + bool header_done = false; + ctrllennum= 0; - header_done = false; for (;;) { struct dpkg_ar_hdr arh; diff --git a/src/deb/info.c b/src/deb/info.c index 794eeb051..38f88aab1 100644 --- a/src/deb/info.c +++ b/src/deb/info.c @@ -85,10 +85,11 @@ info_spew(const char *debar, const char *dir, const char *const *argv) struct dpkg_error err; const char *component; struct varbuf controlfile = VARBUF_INIT; - int fd; int re= 0; while ((component = *argv++) != NULL) { + int fd; + varbuf_reset(&controlfile); varbuf_printf(&controlfile, "%s/%s", dir, component); @@ -117,20 +118,19 @@ info_spew(const char *debar, const char *dir, const char *const *argv) static void info_list(const char *debar, const char *dir) { - char interpreter[INTERPRETER_MAX+1], *p; - int il, lines; struct varbuf controlfile = VARBUF_INIT; - struct dirent **cdlist, *cdep; + struct dirent **cdlist; int cdn, n; FILE *cc; - struct stat stab; - int c; cdn = scandir(dir, &cdlist, &ilist_select, alphasort); if (cdn == -1) ohshite(_("cannot scan directory '%.255s'"), dir); for (n = 0; n < cdn; n++) { + struct dirent *cdep; + struct stat stab; + cdep = cdlist[n]; varbuf_reset(&controlfile); @@ -139,6 +139,9 @@ info_list(const char *debar, const char *dir) if (stat(controlfile.buf, &stab)) ohshite(_("cannot stat '%.255s' (in '%.255s')"), cdep->d_name, dir); if (S_ISREG(stab.st_mode)) { + char interpreter[INTERPRETER_MAX + 1]; + int lines, c; + cc = fopen(controlfile.buf, "r"); if (!cc) ohshite(_("cannot open '%.255s' (in '%.255s')"), cdep->d_name, dir); @@ -146,6 +149,9 @@ info_list(const char *debar, const char *dir) interpreter[0] = '\0'; if (getc(cc) == '#') { if (getc(cc) == '!') { + char *p; + int il; + while ((c= getc(cc))== ' '); p=interpreter; *p++='#'; *p++='!'; il=2; while (il < INTERPRETER_MAX && !c_isspace(c) && c != EOF) { @@ -178,6 +184,8 @@ info_list(const char *debar, const char *dir) ohshite(_("failed to read '%.255s' (in '%.255s')"), CONTROLFILE, dir); warning(_("no 'control' file in control archive!")); } else { + int lines, c; + lines= 1; while ((c= getc(cc))!= EOF) { if (lines) @@ -212,13 +220,14 @@ info_field(const char *debar, const char *dir, const char *const *fields, for (i = 0; fields[i]; i++) { const struct fieldinfo *field; - const struct arbitraryfield *arbfield; varbuf_reset(&str); field = find_field_info(fieldinfos, fields[i]); if (field) { field->wcall(&str, pkg, &pkg->available, fieldflags, field); } else { + const struct arbitraryfield *arbfield; + arbfield = find_arbfield_info(pkg->available.arbs, fields[i]); if (arbfield) varbuf_add_arbfield(&str, arbfield, fieldflags); diff --git a/src/main/archives.c b/src/main/archives.c index 02a088fd1..31b2abd08 100644 --- a/src/main/archives.c +++ b/src/main/archives.c @@ -153,7 +153,6 @@ filesavespackage(struct fsys_namenode_list *file, struct pkginfo *pkgbeinginstalled) { struct fsys_node_pkgs_iter *iter; - struct pkgset *divpkgset; struct pkginfo *thirdpkg; debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s", @@ -164,6 +163,8 @@ filesavespackage(struct fsys_namenode_list *file, * we're installing then they're not actually the same file, so * we can't disappear the package - it is saved by this file. */ if (file->namenode->divert && file->namenode->divert->useinstead) { + struct pkgset *divpkgset; + divpkgset = file->namenode->divert->pkgset; if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) { debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!"); @@ -619,7 +620,6 @@ linktosameexistingdir(const struct tar_entry *ti, const char *fname, { struct stat oldstab, newstab; int statr; - const char *lastslash; statr= stat(fname, &oldstab); if (statr) { @@ -636,6 +636,8 @@ linktosameexistingdir(const struct tar_entry *ti, const char *fname, if (ti->linkname[0] == '/') { varbuf_add_str(symlinkfn, dpkg_fsys_get_dir()); } else { + const char *lastslash; + lastslash= strrchr(fname, '/'); if (lastslash == NULL) internerr("tar entry filename '%s' does not contain '/'", fname); diff --git a/src/main/depcon.c b/src/main/depcon.c index 2990caa96..e8efdd3be 100644 --- a/src/main/depcon.c +++ b/src/main/depcon.c @@ -311,7 +311,6 @@ depisok(struct dependency *dep, struct varbuf *whynot, struct deppossi *possi; struct deppossi *provider; struct pkginfo *pkg_pos; - int nconflicts; /* Use this buffer so that when internationalization comes along we * don't have to rewrite the code completely, only redo the sprintf strings @@ -538,6 +537,8 @@ depisok(struct dependency *dep, struct varbuf *whynot, return false; } else { + int nconflicts; + /* It's conflicts or breaks. There's only one main alternative, * but we also have to consider Providers. We return ‘false’ as soon * as we find something that matches the conflict, and only describe diff --git a/src/main/help.c b/src/main/help.c index c869aac86..b58e6a394 100644 --- a/src/main/help.c +++ b/src/main/help.c @@ -81,7 +81,6 @@ find_command(const char *prog) struct varbuf filename = VARBUF_INIT; const char *path_list; const char *path, *path_end; - size_t path_len; if (prog[0] == '/') return file_is_exec(prog); @@ -91,6 +90,8 @@ find_command(const char *prog) ohshit(_("PATH is not set")); for (path = path_list; path; path = *path_end ? path_end + 1 : NULL) { + size_t path_len; + path_end = strchrnul(path, ':'); path_len = (size_t)(path_end - path); @@ -336,9 +337,10 @@ void pkg_conffiles_mark_old(struct pkginfo *pkg) { const struct conffile *conff; - struct fsys_namenode *namenode; for (conff = pkg->installed.conffiles; conff; conff = conff->next) { + struct fsys_namenode *namenode; + namenode = fsys_hash_find_node(conff->name, 0); /* XXX */ namenode->flags |= FNNF_OLD_CONFF; if (!namenode->oldhash) diff --git a/src/main/main.c b/src/main/main.c index 3a80787d4..02887c296 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -229,9 +229,10 @@ static void set_debug(const struct cmdinfo *cpi, const char *value) { long mask; - const struct debuginfo *dip; if (*value == 'h') { + const struct debuginfo *dip; + printf(_( "%s debugging option, --debug=<octal> or -D<octal>:\n" "\n" diff --git a/src/main/packages.c b/src/main/packages.c index 9c4b75cc6..4fe9d1617 100644 --- a/src/main/packages.c +++ b/src/main/packages.c @@ -171,7 +171,6 @@ packages(const char *const *argv) void process_queue(void) { struct pkg_list *rundown; - struct pkginfo *volatile pkg; volatile enum action action_todo; jmp_buf ejbuf; enum pkg_istobe istobe = PKG_ISTOBE_NORMAL; @@ -219,6 +218,8 @@ void process_queue(void) { } while (!pkg_queue_is_empty(&queue)) { + struct pkginfo *volatile pkg; + pkg = pkg_queue_pop(&queue); if (!pkg) continue; /* Duplicate, which we removed earlier. */ diff --git a/src/main/select.c b/src/main/select.c index de8febd3d..d71202e30 100644 --- a/src/main/select.c +++ b/src/main/select.c @@ -62,8 +62,7 @@ getselections(const char *const *argv) { struct pkg_array array; struct pkginfo *pkg; - const char *thisarg; - int i, found; + int i; modstatdb_open(msdbrw_readonly); @@ -78,8 +77,11 @@ getselections(const char *const *argv) getsel1package(pkg); } } else { + const char *thisarg; + while ((thisarg= *argv++)) { struct pkg_spec pkgspec; + int found; found= 0; pkg_spec_init(&pkgspec, PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD); diff --git a/src/main/trigproc.c b/src/main/trigproc.c index 2ef8a5234..125eb02a0 100644 --- a/src/main/trigproc.c +++ b/src/main/trigproc.c @@ -374,8 +374,6 @@ trigproc(struct pkginfo *pkg, enum trigproc_type type) static struct varbuf namesarg; struct varbuf depwhynot = VARBUF_INIT; - struct trigpend *tp; - struct pkginfo *gaveup; debug(dbg_triggers, "trigproc %s", pkg_name(pkg, pnaw_always)); @@ -385,6 +383,8 @@ trigproc(struct pkginfo *pkg, enum trigproc_type type) pkg->clientdata->trigprocdeferred = NULL; if (pkg->trigpend_head) { + struct pkginfo *gaveup; + struct trigpend *tp; enum dep_check ok; if (pkg->status != PKG_STAT_TRIGGERSPENDING && diff --git a/src/main/unpack.c b/src/main/unpack.c index 98d272f45..9048f6fb0 100644 --- a/src/main/unpack.c +++ b/src/main/unpack.c @@ -854,8 +854,7 @@ static void pkg_update_fields(struct pkginfo *pkg, struct fsys_namenode_queue *newconffiles) { struct dependency *newdeplist, **newdeplistlastp; - struct dependency *newdep, *dep; - struct deppossi **newpossilastp, *possi, *newpossi; + struct dependency *dep; struct conffile **iconffileslastp, *newiconff; struct fsys_namenode_list *cfile; @@ -866,6 +865,9 @@ pkg_update_fields(struct pkginfo *pkg, struct fsys_namenode_queue *newconffiles) newdeplist = NULL; newdeplistlastp = &newdeplist; for (dep = pkg->available.depends; dep; dep = dep->next) { + struct deppossi **newpossilastp, *possi; + struct dependency *newdep; + newdep = nfmalloc(sizeof(*newdep)); newdep->up = pkg; newdep->next = NULL; @@ -873,6 +875,8 @@ pkg_update_fields(struct pkginfo *pkg, struct fsys_namenode_queue *newconffiles) newpossilastp = &newdep->list; for (possi = dep->list; possi; possi = possi->next) { + struct deppossi *newpossi; + newpossi = nfmalloc(sizeof(*newpossi)); newpossi->up = newdep; newpossi->ed = possi->ed; diff --git a/src/main/verify.c b/src/main/verify.c index 4abef9fea..8abe09964 100644 --- a/src/main/verify.c +++ b/src/main/verify.c @@ -114,13 +114,14 @@ static int verify_digest(const char *filename, struct fsys_namenode *fnn, struct verify_checks *checks) { - struct dpkg_error err; static int fd; - char hash[MD5HASHLEN + 1]; fd = open(filename, O_RDONLY); if (fd >= 0) { + struct dpkg_error err; + char hash[MD5HASHLEN + 1]; + push_cleanup(cu_closefd, ehflag_bombout, 1, &fd); if (fd_md5(fd, hash, -1, &err) < 0) ohshit(_("cannot compute MD5 digest for file '%s': %s"), diff --git a/src/query/main.c b/src/query/main.c index 5f1967930..84cae2af6 100644 --- a/src/query/main.c +++ b/src/query/main.c @@ -250,8 +250,6 @@ static int listpackages(const char *const *argv) { struct pkg_array array; - struct pkginfo *pkg; - int i; int rc = 0; struct list_format fmt; struct pager *pager; @@ -269,7 +267,11 @@ listpackages(const char *const *argv) pager = pager_spawn(_("showing package list on pager")); if (!*argv) { + int i; + for (i = 0; i < array.n_pkgs; i++) { + struct pkginfo *pkg; + pkg = array.pkgs[i]; if (pkg->status == PKG_STAT_NOTINSTALLED) array.pkgs[i] = NULL; @@ -332,10 +334,7 @@ searchoutput(struct fsys_namenode *namenode) static int searchfiles(const char *const *argv) { - struct fsys_namenode *namenode; - struct fsys_hash_iter *iter; const char *thisarg; - int found; int failures = 0; struct varbuf path = VARBUF_INIT; static struct varbuf vb; @@ -348,7 +347,8 @@ searchfiles(const char *const *argv) ensure_diversions(); while ((thisarg = *argv++) != NULL) { - found= 0; + struct fsys_namenode *namenode; + int found = 0; if (!strchr("*[?/",*thisarg)) { varbuf_reset(&vb); @@ -369,6 +369,8 @@ searchfiles(const char *const *argv) namenode = fsys_hash_find_node(path.buf, 0); found += searchoutput(namenode); } else { + struct fsys_hash_iter *iter; + iter = fsys_hash_iter_new(); while ((namenode = fsys_hash_iter_next(iter)) != NULL) { if (fnmatch(thisarg,namenode->name,0)) continue; @@ -394,8 +396,6 @@ searchfiles(const char *const *argv) static int print_status(const char *const *argv) { - const char *thisarg; - struct pkginfo *pkg; int failures = 0; modstatdb_open(msdbrw_readonly); @@ -403,7 +403,11 @@ print_status(const char *const *argv) if (!*argv) { writedb_stanzas(stdout, _("<standard output>"), 0); } else { + const char *thisarg; + while ((thisarg = *argv++) != NULL) { + struct pkginfo *pkg; + pkg = dpkg_options_parse_pkgname(cipaction, thisarg); if (pkg->status == PKG_STAT_NOTINSTALLED && @@ -439,8 +443,6 @@ print_status(const char *const *argv) static int print_avail(const char *const *argv) { - const char *thisarg; - struct pkginfo *pkg; int failures = 0; modstatdb_open(msdbrw_readonly | msdbrw_available_readonly); @@ -448,7 +450,11 @@ print_avail(const char *const *argv) if (!*argv) { writedb_stanzas(stdout, _("<standard output>"), wdb_dump_available); } else { + const char *thisarg; + while ((thisarg = *argv++) != NULL) { + struct pkginfo *pkg; + pkg = dpkg_options_parse_pkgname(cipaction, thisarg); if (!pkg_is_informative(pkg, &pkg->available)) { @@ -560,10 +566,8 @@ showpackages(const char *const *argv) { struct dpkg_error err; struct pkg_array array; - struct pkginfo *pkg; struct pkg_format_node *fmt; bool fmt_needs_db_fsys; - int i; int rc = 0; fmt = pkg_format_parse(showformat, &err); @@ -585,9 +589,13 @@ showpackages(const char *const *argv) pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch); if (!*argv) { + int i; + if (fmt_needs_db_fsys) ensure_allinstfiles_available_quiet(); for (i = 0; i < array.n_pkgs; i++) { + struct pkginfo *pkg; + pkg = array.pkgs[i]; if (pkg->status == PKG_STAT_NOTINSTALLED) continue; diff --git a/src/split/join.c b/src/split/join.c index 51de444bc..177d52b78 100644 --- a/src/split/join.c +++ b/src/split/join.c @@ -38,7 +38,7 @@ void reassemble(struct partinfo **partlist, const char *outputfile) { struct dpkg_error err; - int fd_out, fd_in; + int fd_out; int i; printf(P_("Putting package %s together from %d part: ", @@ -51,6 +51,7 @@ void reassemble(struct partinfo **partlist, const char *outputfile) { ohshite(_("unable to open output file '%.250s'"), outputfile); for (i=0; i<partlist[0]->maxpartn; i++) { struct partinfo *pi = partlist[i]; + int fd_in; fd_in = open(pi->filename, O_RDONLY); if (fd_in < 0) diff --git a/t/cppcheck/cppcheck.supp b/t/cppcheck/cppcheck.supp index c1b61eac9..4ed6c2191 100644 --- a/t/cppcheck/cppcheck.supp +++ b/t/cppcheck/cppcheck.supp @@ -5,9 +5,6 @@ // We assume that the system has a sane NULL definition. varFuncNullUB -// TODO: While perhaps valid there are many to handle right away. -variableScope - // TODO: While perhaps valid there are many to handle right away. constParameter -- Dpkg.Org's dpkg