Use stpeprintf() where appropriate

This function allows reducing error checking (since errors are
propagated across chained calls), and also simplifies the calculation of
the start and end of the buffer where the string should be written.

Moreover, the new code is more optimized, since many calls to strlen(3)
have been removed.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar 2023-01-30 00:54:07 +01:00 committed by Iker Pedrosa
parent 7e213cfb50
commit 46610792e9
2 changed files with 40 additions and 38 deletions

View File

@ -12,6 +12,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "prototypes.h" #include "prototypes.h"
#include "stpeprintf.h"
#include "idmapping.h" #include "idmapping.h"
#if HAVE_SYS_CAPABILITY_H #if HAVE_SYS_CAPABILITY_H
#include <sys/prctl.h> #include <sys/prctl.h>
@ -141,7 +142,7 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
int idx; int idx;
const struct map_range *mapping; const struct map_range *mapping;
size_t bufsize; size_t bufsize;
char *buf, *pos; char *buf, *pos, *end;
int fd; int fd;
#if HAVE_SYS_CAPABILITY_H #if HAVE_SYS_CAPABILITY_H
@ -189,21 +190,20 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
bufsize = ranges * ((ULONG_DIGITS + 1) * 3); bufsize = ranges * ((ULONG_DIGITS + 1) * 3);
pos = buf = xmalloc(bufsize); pos = buf = xmalloc(bufsize);
end = buf + bufsize;
/* Build the mapping command */ /* Build the mapping command */
mapping = mappings; mapping = mappings;
for (idx = 0; idx < ranges; idx++, mapping++) { for (idx = 0; idx < ranges; idx++, mapping++) {
/* Append this range to the string that will be written */ /* Append this range to the string that will be written */
int written = snprintf(pos, bufsize - (pos - buf), pos = stpeprintf(pos, end, "%lu %lu %lu\n",
"%lu %lu %lu\n", mapping->upper,
mapping->upper, mapping->lower,
mapping->lower, mapping->count);
mapping->count); }
if ((written <= 0) || ((size_t)written >= (bufsize - (pos - buf)))) { if (pos == end || pos == NULL) {
fprintf(log_get_logfd(), _("%s: snprintf failed!\n"), log_get_progname()); fprintf(log_get_logfd(), _("%s: stpeprintf failed!\n"), log_get_progname());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
pos += written;
} }
/* Write the mapping to the mapping file */ /* Write the mapping to the mapping file */

View File

@ -35,6 +35,7 @@
#include "sgroupio.h" #include "sgroupio.h"
#endif #endif
#include "shadowlog.h" #include "shadowlog.h"
#include "stpeprintf.h"
/* /*
* exit status values * exit status values
*/ */
@ -543,46 +544,49 @@ static void close_files (void)
*/ */
static void prepare_failure_reports (void) static void prepare_failure_reports (void)
{ {
char *gr, *gr_end;
#ifdef SHADOWGRP
char *sgr, *sgr_end;
#endif
char *pw, *pw_end;
info_group.name = group_name; info_group.name = group_name;
#ifdef SHADOWGRP #ifdef SHADOWGRP
info_gshadow.name = group_name; info_gshadow.name = group_name;
#endif #endif
info_passwd.name = group_name; info_passwd.name = group_name;
info_group.audit_msg = xmalloc (512); gr = xmalloc (512);
info_group.audit_msg = gr;
gr_end = gr + 512;
#ifdef SHADOWGRP #ifdef SHADOWGRP
info_gshadow.audit_msg = xmalloc (512); sgr = xmalloc (512);
info_gshadow.audit_msg = sgr;
sgr_end = sgr + 512;
#endif #endif
info_passwd.audit_msg = xmalloc (512); pw = xmalloc (512);
info_passwd.audit_msg = pw;
pw_end = pw + 512;
(void) snprintf (info_group.audit_msg, 512, gr = stpeprintf(gr, gr_end, "changing %s; ", gr_dbname ());
"changing %s; ", gr_dbname ());
#ifdef SHADOWGRP #ifdef SHADOWGRP
(void) snprintf (info_gshadow.audit_msg, 512, sgr = stpeprintf(sgr, sgr_end, "changing %s; ", sgr_dbname ());
"changing %s; ", sgr_dbname ());
#endif #endif
(void) snprintf (info_passwd.audit_msg, 512, pw = stpeprintf(pw, pw_end, "changing %s; ", pw_dbname ());
"changing %s; ", pw_dbname ());
info_group.action = info_group.audit_msg info_group.action = gr;
+ strlen (info_group.audit_msg);
#ifdef SHADOWGRP #ifdef SHADOWGRP
info_gshadow.action = info_gshadow.audit_msg info_gshadow.action = sgr;
+ strlen (info_gshadow.audit_msg);
#endif #endif
info_passwd.action = info_passwd.audit_msg info_passwd.action = pw;
+ strlen (info_passwd.audit_msg);
(void) snprintf (info_group.action, gr = stpeprintf(gr, gr_end,
512 - strlen (info_group.audit_msg),
"group %s/%ju", group_name, (uintmax_t) group_id); "group %s/%ju", group_name, (uintmax_t) group_id);
#ifdef SHADOWGRP #ifdef SHADOWGRP
(void) snprintf (info_gshadow.action, sgr = stpeprintf(sgr, sgr_end,
512 - strlen (info_gshadow.audit_msg),
"group %s", group_name); "group %s", group_name);
#endif #endif
(void) snprintf (info_passwd.action, pw = stpeprintf(pw, pw_end,
512 - strlen (info_passwd.audit_msg),
"group %s/%ju", group_name, (uintmax_t) group_id); "group %s/%ju", group_name, (uintmax_t) group_id);
if (nflg) { if (nflg) {
@ -615,15 +619,13 @@ static void prepare_failure_reports (void)
if (gflg) { if (gflg) {
strncat (info_group.action, ", new gid: ", strncat (info_group.action, ", new gid: ",
511 - strlen (info_group.audit_msg)); 511 - strlen (info_group.audit_msg));
(void) snprintf (info_group.action+strlen (info_group.action), stpeprintf(info_group.action+strlen (info_group.action),
512 - strlen (info_group.audit_msg), gr_end, "%ju", (uintmax_t) group_newid);
"%ju", (uintmax_t) group_newid);
strncat (info_passwd.action, ", new gid: ", strncat (info_passwd.action, ", new gid: ",
511 - strlen (info_passwd.audit_msg)); 511 - strlen (info_passwd.audit_msg));
(void) snprintf (info_passwd.action+strlen (info_passwd.action), stpeprintf(info_passwd.action+strlen (info_passwd.action),
512 - strlen (info_passwd.audit_msg), pw_end, "%ju", (uintmax_t) group_newid);
"%ju", (uintmax_t) group_newid);
} }
info_group.audit_msg[511] = '\0'; info_group.audit_msg[511] = '\0';
#ifdef SHADOWGRP #ifdef SHADOWGRP