From: Alejandro Colomar Date: Tue, 14 Oct 2025 10:49:08 +0000 (+0200) Subject: */: s/SNPRINTF/stprintf_a/ X-Git-Tag: 4.19.0-rc1~73 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ce044d17914b6d2d883c60c99bd4b7c009766c20;p=thirdparty%2Fshadow.git */: s/SNPRINTF/stprintf_a/ This name better reflects that it handles arrays, and doesn't shout. Signed-off-by: Alejandro Colomar --- diff --git a/lib/audit_help.c b/lib/audit_help.c index 68835091e..b0ac5631e 100644 --- a/lib/audit_help.c +++ b/lib/audit_help.c @@ -104,10 +104,10 @@ audit_logger_with_group(int type, const char *op, const char *name, len = strnlen(grp, sizeof(enc_group)/2); if (audit_value_needs_encoding(grp, len)) { - SNPRINTF(buf, "%s %s=%s", op, grp_type, + stprintf_a(buf, "%s %s=%s", op, grp_type, audit_encode_value(enc_group, grp, len)); } else { - SNPRINTF(buf, "%s %s=\"%s\"", op, grp_type, grp); + stprintf_a(buf, "%s %s=\"%s\"", op, grp_type, grp); } audit_log_acct_message(audit_fd, type, NULL, buf, name, id, diff --git a/lib/commonio.c b/lib/commonio.c index 73afa6098..d37ba23c9 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -140,7 +140,7 @@ static int do_lock_file (const char *file, const char *lock, bool log) } pid = getpid (); - SNPRINTF(buf, "%lu", (unsigned long) pid); + stprintf_a(buf, "%lu", (unsigned long) pid); len = (ssize_t) strlen (buf) + 1; if (write_full(fd, buf, len) == -1) { if (log) { @@ -340,7 +340,7 @@ static void free_linked_list (struct commonio_db *db) int commonio_setname (struct commonio_db *db, const char *name) { - SNPRINTF(db->filename, "%s", name); + stprintf_a(db->filename, "%s", name); db->setname = true; return 1; } @@ -486,7 +486,7 @@ int commonio_unlock (struct commonio_db *db, bool process_selinux) * then call ulckpwdf() (if used) on last unlock. */ db->locked = false; - SNPRINTF(lock, "%s.lock", db->filename); + stprintf_a(lock, "%s.lock", db->filename); unlink (lock); dec_lock_count (); return 1; @@ -916,7 +916,7 @@ int commonio_close (struct commonio_db *db, bool process_selinux) /* * Create backup file. */ - if (SNPRINTF(buf, "%s-", db->filename) == -1) { + if (stprintf_a(buf, "%s-", db->filename) == -1) { (void) fclose (db->fp); db->fp = NULL; goto fail; @@ -955,7 +955,7 @@ int commonio_close (struct commonio_db *db, bool process_selinux) sb.st_gid = db->st_gid; } - if (SNPRINTF(buf, "%s+", db->filename) == -1) + if (stprintf_a(buf, "%s+", db->filename) == -1) goto fail; #ifdef WITH_SELINUX diff --git a/lib/env.c b/lib/env.c index 91424087d..37951b3b2 100644 --- a/lib/env.c +++ b/lib/env.c @@ -170,7 +170,7 @@ void set_env (int argc, char *const *argv) cp = strchr (*argv, '='); if (NULL == cp) { - assert(SNPRINTF(variable, "L%d", noname) != -1); + assert(stprintf_a(variable, "L%d", noname) != -1); noname++; addenv (variable, *argv); } else { diff --git a/lib/get_pid.c b/lib/get_pid.c index 7f80b1645..6ccc79d79 100644 --- a/lib/get_pid.c +++ b/lib/get_pid.c @@ -59,7 +59,7 @@ int open_pidfd(const char *pidstr) if (get_pid(pidstr, &target) == -1) return -ENOENT; - if (SNPRINTF(proc_dir_name, "/proc/%d/", target) == -1) { + if (stprintf_a(proc_dir_name, "/proc/%d/", target) == -1) { fprintf(stderr, "snprintf of proc path failed for %d: %s\n", target, strerrno()); return -EINVAL; diff --git a/lib/hushed.c b/lib/hushed.c index 56add9e96..530996f40 100644 --- a/lib/hushed.c +++ b/lib/hushed.c @@ -60,7 +60,7 @@ bool hushed (const char *username) */ if (hushfile[0] != '/') { - SNPRINTF(buf, "%s/%s", pw->pw_dir, hushfile); + stprintf_a(buf, "%s/%s", pw->pw_dir, hushfile); return (access (buf, F_OK) == 0); } diff --git a/lib/nss.c b/lib/nss.c index 9f30b0a11..21a149db0 100644 --- a/lib/nss.c +++ b/lib/nss.c @@ -109,7 +109,7 @@ nss_init(const char *nsswitch_path) { fprintf(shadow_logfd, "Using files\n"); goto null_subid; } - SNPRINTF(libname, "libsubid_%s.so", p); + stprintf_a(libname, "libsubid_%s.so", p); h = dlopen(libname, RTLD_LAZY); if (!h) { fprintf(shadow_logfd, "Error opening %s: %s\n", libname, dlerror()); diff --git a/lib/pwauth.c b/lib/pwauth.c index 3a2ccb296..0f0d2ff77 100644 --- a/lib/pwauth.c +++ b/lib/pwauth.c @@ -111,7 +111,7 @@ pw_auth(const char *cipher, const char *user) } #endif - SNPRINTF(prompt, cp, user); + stprintf_a(prompt, cp, user); clear = agetpass(prompt); input = (clear == NULL) ? "" : clear; diff --git a/lib/shell.c b/lib/shell.c index 40b840ee9..1e49df7b0 100644 --- a/lib/shell.c +++ b/lib/shell.c @@ -48,7 +48,7 @@ int shell (const char *file, /*@null@*/const char *arg, char *const envp[]) * don't want to tell us what it is themselves. */ if (arg == NULL) { - SNPRINTF(arg0, "-%s", Basename(file)); + stprintf_a(arg0, "-%s", Basename(file)); arg = arg0; } @@ -74,7 +74,7 @@ int shell (const char *file, /*@null@*/const char *arg, char *const envp[]) * how to execute this stupid shell, so I might as well give * up in disgust ... */ - SNPRINTF(arg0, _("Cannot execute %s"), file); + stprintf_a(arg0, _("Cannot execute %s"), file); errno = err; perror (arg0); return err; diff --git a/lib/string/README b/lib/string/README index dfe6465c5..37eb4f1d7 100644 --- a/lib/string/README +++ b/lib/string/README @@ -213,7 +213,7 @@ sprintf/ - Formatted string creation snprintf(3) wrapper that reports truncation with -1. If you need more than one call to form a string, use seprintf() instead. - STPRINTF() // Current name: SNPRINTF() + stprintf_a() Like stprintf(), but takes an array. seprintf() // Current name: stpeprintf()) diff --git a/lib/string/sprintf/snprintf.h b/lib/string/sprintf/snprintf.h index 94f7ee3ca..f6dee1245 100644 --- a/lib/string/sprintf/snprintf.h +++ b/lib/string/sprintf/snprintf.h @@ -16,7 +16,8 @@ #include "sizeof.h" -#define SNPRINTF(s, fmt, ...) \ +// stprintf_a - string truncate print formatted array +#define stprintf_a(s, fmt, ...) \ ( \ snprintf_(s, countof(s), fmt __VA_OPT__(,) __VA_ARGS__) \ ) diff --git a/lib/subordinateio.c b/lib/subordinateio.c index 0a6835e75..37f6ab2f8 100644 --- a/lib/subordinateio.c +++ b/lib/subordinateio.c @@ -226,7 +226,7 @@ static const struct subordinate_range *find_range(struct commonio_db *db, return NULL; } owner_uid = pwd->pw_uid; - if (SNPRINTF(owner_uid_string, "%lu", (unsigned long) owner_uid) == -1) + if (stprintf_a(owner_uid_string, "%lu", (unsigned long) owner_uid) == -1) return NULL; commonio_rewind(db); diff --git a/lib/user_busy.c b/lib/user_busy.c index d9103c539..09bf8c811 100644 --- a/lib/user_busy.c +++ b/lib/user_busy.c @@ -98,7 +98,7 @@ static int different_namespace (const char *sname) char path[41]; char buf[512], buf2[512]; - SNPRINTF(path, "/proc/%s/ns/user", sname); + stprintf_a(path, "/proc/%s/ns/user", sname); if (readlinknul_a(path, buf) == -1) return 0; @@ -121,7 +121,7 @@ static int check_status (const char *name, const char *sname, uid_t uid) char line[1024]; FILE *sfile; - SNPRINTF(status, "/proc/%s/status", sname); + stprintf_a(status, "/proc/%s/status", sname); sfile = fopen (status, "r"); if (NULL == sfile) { @@ -214,7 +214,7 @@ static int user_busy_processes (const char *name, uid_t uid) } /* Check if the process is in our chroot */ - SNPRINTF(root_path, "/proc/%lu/root", (unsigned long) pid); + stprintf_a(root_path, "/proc/%lu/root", (unsigned long) pid); if (stat (root_path, &sbroot_process) != 0) { continue; } @@ -234,7 +234,7 @@ static int user_busy_processes (const char *name, uid_t uid) return 1; } - SNPRINTF(task_path, "/proc/%lu/task", (unsigned long) pid); + stprintf_a(task_path, "/proc/%lu/task", (unsigned long) pid); task_dir = opendir (task_path); if (task_dir != NULL) { while (NULL != (ent = readdir(task_dir))) { diff --git a/src/chage.c b/src/chage.c index 8555db8ae..d1b04b4dd 100644 --- a/src/chage.c +++ b/src/chage.c @@ -171,12 +171,12 @@ static int new_fields (void) (void) puts (_("Enter the new value, or press ENTER for the default")); (void) puts (""); - SNPRINTF(buf, "%ld", mindays); + stprintf_a(buf, "%ld", mindays); change_field (buf, sizeof buf, _("Minimum Password Age")); if (a2sl(&mindays, buf, NULL, 0, -1, LONG_MAX) == -1) return 0; - SNPRINTF(buf, "%ld", maxdays); + stprintf_a(buf, "%ld", maxdays); change_field (buf, sizeof buf, _("Maximum Password Age")); if (a2sl(&maxdays, buf, NULL, 0, -1, LONG_MAX) == -1) return 0; @@ -197,12 +197,12 @@ static int new_fields (void) } } - SNPRINTF(buf, "%ld", warndays); + stprintf_a(buf, "%ld", warndays); change_field (buf, sizeof buf, _("Password Expiration Warning")); if (a2sl(&warndays, buf, NULL, 0, -1, LONG_MAX) == -1) return 0; - SNPRINTF(buf, "%ld", inactdays); + stprintf_a(buf, "%ld", inactdays); change_field (buf, sizeof buf, _("Password Inactive")); if (a2sl(&inactdays, buf, NULL, 0, -1, LONG_MAX) == -1) return 0; diff --git a/src/gpasswd.c b/src/gpasswd.c index 6dec40d66..1224d80a8 100644 --- a/src/gpasswd.c +++ b/src/gpasswd.c @@ -476,7 +476,7 @@ static void log_gpasswd_failure_group (MAYBE_UNUSED void *arg) { char buf[1024]; - SNPRINTF(buf, " in %s", gr_dbname()); + stprintf_a(buf, " in %s", gr_dbname()); log_gpasswd_failure (buf); } @@ -485,7 +485,7 @@ static void log_gpasswd_failure_gshadow (MAYBE_UNUSED void *arg) { char buf[1024]; - SNPRINTF(buf, " in %s", sgr_dbname()); + stprintf_a(buf, " in %s", sgr_dbname()); log_gpasswd_failure (buf); } #endif /* SHADOWGRP */ @@ -521,7 +521,7 @@ static void log_gpasswd_success (const char *suffix) "password of group %s removed by %s%s", group, myname, suffix)); #ifdef WITH_AUDIT - SNPRINTF(buf, "password of group %s removed by %s%s", + stprintf_a(buf, "password of group %s removed by %s%s", group, myname, suffix); audit_logger_with_group (AUDIT_GRP_CHAUTHTOK, "delete-group-password", @@ -533,7 +533,7 @@ static void log_gpasswd_success (const char *suffix) "access to group %s restricted by %s%s", group, myname, suffix)); #ifdef WITH_AUDIT - SNPRINTF(buf, "access to group %s restricted by %s%s", + stprintf_a(buf, "access to group %s restricted by %s%s", group, myname, suffix); audit_logger_with_group (AUDIT_GRP_MGMT, "restrict-group", @@ -587,7 +587,7 @@ static void log_gpasswd_success_group (MAYBE_UNUSED void *arg) { char buf[1024]; - SNPRINTF(buf, " in %s", gr_dbname()); + stprintf_a(buf, " in %s", gr_dbname()); log_gpasswd_success (buf); } diff --git a/src/login.c b/src/login.c index 9f193b004..dfc0f96c6 100644 --- a/src/login.c +++ b/src/login.c @@ -590,16 +590,16 @@ int main (int argc, char **argv) } if (!streq(cp, "")) { - SNPRINTF(fromhost, " on '%.100s' from '%.200s'", tty, cp); + stprintf_a(fromhost, " on '%.100s' from '%.200s'", tty, cp); } else { - SNPRINTF(fromhost, " on '%.100s'", tty); + stprintf_a(fromhost, " on '%.100s'", tty); } free(host); top: /* only allow ALARM sec. for login */ timeout = getdef_unum ("LOGIN_TIMEOUT", ALARM); - SNPRINTF(tmsg, _("\nLogin timed out after %u seconds.\n"), timeout); + stprintf_a(tmsg, _("\nLogin timed out after %u seconds.\n"), timeout); (void) signal (SIGALRM, alarm_handler); if (timeout > 0) { (void) alarm (timeout); @@ -644,7 +644,7 @@ int main (int argc, char **argv) /* Make the login prompt look like we want it */ if (gethostname (hostn, sizeof (hostn)) == 0) { - SNPRINTF(loginprompt, _("%s login: "), hostn); + stprintf_a(loginprompt, _("%s login: "), hostn); } else { STRTCPY(loginprompt, _("login: ")); } diff --git a/src/newgrp.c b/src/newgrp.c index 13b6ac17b..325a9141b 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -199,7 +199,7 @@ static void check_perms (const struct group *grp, if (streq(grp->gr_passwd, "") || !streq(grp->gr_passwd, cpasswd)) { #ifdef WITH_AUDIT - SNPRINTF(audit_buf, "authentication new_gid=%lu", + stprintf_a(audit_buf, "authentication new_gid=%lu", (unsigned long) grp->gr_gid); audit_logger (AUDIT_GRP_AUTH, audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE); @@ -212,7 +212,7 @@ static void check_perms (const struct group *grp, goto failure; } #ifdef WITH_AUDIT - SNPRINTF(audit_buf, "authentication new_gid=%lu", + stprintf_a(audit_buf, "authentication new_gid=%lu", (unsigned long) grp->gr_gid); audit_logger (AUDIT_GRP_AUTH, audit_buf, NULL, getuid (), SHADOW_AUDIT_SUCCESS); @@ -699,7 +699,7 @@ int main (int argc, char **argv) if (setgid (gid) != 0) { perror ("setgid"); #ifdef WITH_AUDIT - SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid); + stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid); audit_logger (AUDIT_CHGRP_ID, audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE); #endif @@ -709,7 +709,7 @@ int main (int argc, char **argv) if (setuid (getuid ()) != 0) { perror ("setuid"); #ifdef WITH_AUDIT - SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid); + stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid); audit_logger (AUDIT_CHGRP_ID, audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE); #endif @@ -724,7 +724,7 @@ int main (int argc, char **argv) closelog (); execl (SHELL, "sh", "-c", command, (char *) NULL); #ifdef WITH_AUDIT - SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid); + stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid); audit_logger (AUDIT_CHGRP_ID, audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE); #endif @@ -792,7 +792,7 @@ int main (int argc, char **argv) } #ifdef WITH_AUDIT - SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid); + stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid); audit_logger (AUDIT_CHGRP_ID, audit_buf, NULL, getuid (), SHADOW_AUDIT_SUCCESS); #endif diff --git a/src/newusers.c b/src/newusers.c index 70e9eb7d7..818f73fa2 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -736,7 +736,7 @@ static void process_flags (int argc, char **argv, struct option_flags *flags) if (freopen (argv[optind], "r", stdin) == NULL) { char buf[BUFSIZ]; - SNPRINTF(buf, "%s: %s", Prog, argv[1]); + stprintf_a(buf, "%s: %s", Prog, argv[1]); perror (buf); fail_exit (EXIT_FAILURE, !flags->chroot); } diff --git a/src/su.c b/src/su.c index 98ced92a0..2cfb2cf56 100644 --- a/src/su.c +++ b/src/su.c @@ -394,8 +394,8 @@ static void prepare_pam_close_session (void) stderr); (void) kill (-pid_child, caught); - SNPRINTF(kill_msg, _(" ...killed.\n")); - SNPRINTF(wait_msg, _(" ...waiting for child to terminate.\n")); + stprintf_a(kill_msg, _(" ...killed.\n")); + stprintf_a(wait_msg, _(" ...waiting for child to terminate.\n")); /* Any signals other than SIGCHLD and SIGALRM will no longer have any effect, * so it's time to block all of them. */ diff --git a/src/useradd.c b/src/useradd.c index d4883b5ef..efc366c9d 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -690,7 +690,7 @@ set_defaults(void) /* * Rename the current default file to its backup name. */ - assert(SNPRINTF(buf, "%s-", default_file) != -1); + assert(stprintf_a(buf, "%s-", default_file) != -1); unlink (buf); if ((link (default_file, buf) != 0) && (ENOENT != errno)) { fprintf (stderr, diff --git a/src/vipw.c b/src/vipw.c index 83ff3d92b..a3d2d7f48 100644 --- a/src/vipw.c +++ b/src/vipw.c @@ -206,7 +206,7 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (bool)) /* FIXME: the following should have variable sizes */ char filebackup[1024], fileedit[1024]; - SNPRINTF(filebackup, "%s-", file); + stprintf_a(filebackup, "%s-", file); #ifdef WITH_TCB if (tcb_mode) { if ( (mkdir (TCB_DIR "/" SHADOWTCB_SCRATCHDIR, 0700) != 0) @@ -216,12 +216,12 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (bool)) if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) { vipwexit (_("failed to drop privileges"), errno, 1); } - SNPRINTF(fileedit, + stprintf_a(fileedit, TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s", user); } else { #endif /* WITH_TCB */ - SNPRINTF(fileedit, "%s.edit", file); + stprintf_a(fileedit, "%s.edit", file); #ifdef WITH_TCB } #endif /* WITH_TCB */ diff --git a/tests/unit/test_snprintf.c b/tests/unit/test_snprintf.c index fb22d6d16..f873de627 100644 --- a/tests/unit/test_snprintf.c +++ b/tests/unit/test_snprintf.c @@ -17,16 +17,16 @@ #include "string/sprintf/snprintf.h" -static void test_SNPRINTF_trunc(void **state); -static void test_SNPRINTF_ok(void **state); +static void test_stprintf_a_trunc(void **state); +static void test_stprintf_a_ok(void **state); int main(void) { const struct CMUnitTest tests[] = { - cmocka_unit_test(test_SNPRINTF_trunc), - cmocka_unit_test(test_SNPRINTF_ok), + cmocka_unit_test(test_stprintf_a_trunc), + cmocka_unit_test(test_stprintf_a_ok), }; return cmocka_run_group_tests(tests, NULL, NULL); @@ -34,33 +34,33 @@ main(void) static void -test_SNPRINTF_trunc(void **state) +test_stprintf_a_trunc(void **state) { char buf[countof("foo")]; // Test that we're not returning SIZE_MAX - assert_true(SNPRINTF(buf, "f%su", "oo") < 0); + assert_true(stprintf_a(buf, "f%su", "oo") < 0); assert_true(strcmp(buf, "foo") == 0); - assert_true(SNPRINTF(buf, "barbaz") == -1); + assert_true(stprintf_a(buf, "barbaz") == -1); assert_true(strcmp(buf, "bar") == 0); } static void -test_SNPRINTF_ok(void **state) +test_stprintf_a_ok(void **state) { char buf[countof("foo")]; - assert_true(SNPRINTF(buf, "%s", "foo") == strlen("foo")); + assert_true(stprintf_a(buf, "%s", "foo") == strlen("foo")); assert_true(strcmp(buf, "foo") == 0); - assert_true(SNPRINTF(buf, "%do", 1) == strlen("1o")); + assert_true(stprintf_a(buf, "%do", 1) == strlen("1o")); assert_true(strcmp(buf, "1o") == 0); - assert_true(SNPRINTF(buf, "f") == strlen("f")); + assert_true(stprintf_a(buf, "f") == strlen("f")); assert_true(strcmp(buf, "f") == 0); - assert_true(SNPRINTF(buf, "") == strlen("")); + assert_true(stprintf_a(buf, "") == strlen("")); assert_true(strcmp(buf, "") == 0); }