Skip to content

Commit

Permalink
lib/, src/: Rename some local variables
Browse files Browse the repository at this point in the history
'endptr' is appropriate internally in strtol(3) because it's a pointer
to 'end', and 'end' itself is a pointer to one-after-the-last character
of the numeric string.  In other words,

	endptr == &end

However, naming the pointer whose address we pass to strtol(3)'s
'endptr' feels wrong, and causes me trouble while parsing the code; I
need to double check the number of dereferences, because something feels
wrong in my head.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed May 4, 2024
1 parent f40bd94 commit 98aefe8
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 44 deletions.
6 changes: 3 additions & 3 deletions lib/get_gid.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
int
get_gid(const char *gidstr, gid_t *gid)
{
char *end;
long long val;
char *endptr;

errno = 0;
val = strtoll(gidstr, &endptr, 10);
val = strtoll(gidstr, &end, 10);
if ( ('\0' == *gidstr)
|| ('\0' != *endptr)
|| ('\0' != *end)
|| (0 != errno)
|| (/*@+longintegral@*/val != (gid_t)val)/*@=longintegral@*/) {
return -1;
Expand Down
19 changes: 10 additions & 9 deletions lib/get_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
#include "string/sprintf.h"


int get_pid (const char *pidstr, pid_t *pid)
int
get_pid(const char *pidstr, pid_t *pid)
{
char *end;
long long val;
char *endptr;

errno = 0;
val = strtoll(pidstr, &endptr, 10);
val = strtoll(pidstr, &end, 10);
if ( ('\0' == *pidstr)
|| ('\0' != *endptr)
|| ('\0' != *end)
|| (0 != errno)
|| (val < 1)
|| (/*@+longintegral@*/val != (pid_t)val)/*@=longintegral@*/) {
Expand All @@ -43,15 +44,15 @@ int get_pid (const char *pidstr, pid_t *pid)
*/
int get_pidfd_from_fd(const char *pidfdstr)
{
long long val;
char *endptr;
struct stat st;
char *end;
long long val;
struct stat st;
dev_t proc_st_dev, proc_st_rdev;

errno = 0;
val = strtoll(pidfdstr, &endptr, 10);
val = strtoll(pidfdstr, &end, 10);
if ( ('\0' == *pidfdstr)
|| ('\0' != *endptr)
|| ('\0' != *end)
|| (0 != errno)
|| (val < 0)
|| (/*@+longintegral@*/val != (int)val)/*@=longintegral@*/) {
Expand Down
6 changes: 3 additions & 3 deletions lib/get_uid.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
int
get_uid(const char *uidstr, uid_t *uid)
{
char *end;
long long val;
char *endptr;

errno = 0;
val = strtoll(uidstr, &endptr, 10);
val = strtoll(uidstr, &end, 10);
if ( ('\0' == *uidstr)
|| ('\0' != *endptr)
|| ('\0' != *end)
|| (0 != errno)
|| (/*@+longintegral@*/val != (uid_t)val)/*@=longintegral@*/) {
return -1;
Expand Down
6 changes: 3 additions & 3 deletions lib/getgr_nam_gid.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
*/
extern /*@only@*//*@null@*/struct group *getgr_nam_gid (/*@null@*/const char *grname)
{
char *end;
long long gid;
char *endptr;

if (NULL == grname) {
return NULL;
}

errno = 0;
gid = strtoll(grname, &endptr, 10);
gid = strtoll(grname, &end, 10);
if ( ('\0' != *grname)
&& ('\0' == *endptr)
&& ('\0' == *end)
&& (0 == errno)
&& (/*@+longintegral@*/gid == (gid_t)gid)/*@=longintegral@*/) {
return xgetgrgid (gid);
Expand Down
18 changes: 9 additions & 9 deletions lib/getrange.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ getrange(const char *range,
unsigned long *min, bool *has_min,
unsigned long *max, bool *has_max)
{
char *endptr;
char *end;

if (NULL == range)
return -1;
Expand All @@ -39,32 +39,32 @@ getrange(const char *range,
*has_max = false;

if ('-' == range[0]) {
endptr = range + 1;
end = range + 1;
goto parse_max;
}

errno = 0;
*min = strtoul_noneg(range, &endptr, 10);
if (endptr == range || 0 != errno)
*min = strtoul_noneg(range, &end, 10);
if (end == range || 0 != errno)
return -1;
*has_min = true;

switch (*endptr++) {
switch (*end++) {
case '\0':
*has_max = true;
*max = *min;
return 0; /* <long> */

case '-':
if ('\0' == *endptr)
if ('\0' == *end)
return 0; /* <long>- */
parse_max:
if (!isdigit(*endptr))
if (!isdigit(*end))
return -1;

errno = 0;
*max = strtoul_noneg(endptr, &endptr, 10);
if ('\0' != *endptr || 0 != errno)
*max = strtoul_noneg(end, &end, 10);
if ('\0' != *end || 0 != errno)
return -1;
*has_max = true;

Expand Down
12 changes: 6 additions & 6 deletions lib/gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
/*@observer@*/time_t gettime (void)
{
char *endptr;
char *end;
char *source_date_epoch;
time_t fallback;
unsigned long long epoch;
Expand All @@ -39,19 +39,19 @@
return fallback;

errno = 0;
epoch = strtoull_noneg(source_date_epoch, &endptr, 10);
epoch = strtoull_noneg(source_date_epoch, &end, 10);
if (errno != 0) {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: strtoull: %s\n"),
strerror(errno));
} else if (endptr == source_date_epoch) {
} else if (end == source_date_epoch) {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n"),
endptr);
} else if (*endptr != '\0') {
end);
} else if (*end != '\0') {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n"),
endptr);
end);
} else if (epoch > ULONG_MAX) {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to %lu but was found to be: %llu\n"),
Expand Down
6 changes: 3 additions & 3 deletions lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static int setrlimit_value (unsigned int resource,
const char *value,
unsigned int multiplier)
{
char *endptr;
char *end;
long l;
rlim_t limit;
struct rlimit rlim;
Expand All @@ -67,9 +67,9 @@ static int setrlimit_value (unsigned int resource,
* work with the limit string parser as is anyway)
*/
errno = 0;
l = strtol(value, &endptr, 10);
l = strtol(value, &end, 10);

if (value == endptr || errno != 0)
if (value == end || errno != 0)
return 0; // FIXME: We could instead throw an error, though.

if (__builtin_mul_overflow(l, multiplier, &limit)) {
Expand Down
10 changes: 5 additions & 5 deletions lib/prefix_flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ extern void prefix_endgrent(void)

extern struct group *prefix_getgr_nam_gid(const char *grname)
{
long long gid;
char *endptr;
struct group *g;
char *end;
long long gid;
struct group *g;

if (NULL == grname) {
return NULL;
Expand All @@ -346,9 +346,9 @@ extern struct group *prefix_getgr_nam_gid(const char *grname)
return getgr_nam_gid(grname);

errno = 0;
gid = strtoll(grname, &endptr, 10);
gid = strtoll(grname, &end, 10);
if ( ('\0' != *grname)
&& ('\0' == *endptr)
&& ('\0' == *end)
&& (0 == errno)
&& (gid == (gid_t)gid))
{
Expand Down
6 changes: 3 additions & 3 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,14 +857,14 @@ static int get_groups (char *list)
*/
static struct group * get_local_group(char * grp_name)
{
char *end;
const struct group *grp;
struct group *result_grp = NULL;
long long gid;
char *endptr;

gid = strtoll (grp_name, &endptr, 10);
gid = strtoll(grp_name, &end, 10);
if ( ('\0' != *grp_name)
&& ('\0' == *endptr)
&& ('\0' == *end)
&& (ERANGE != errno)
&& (gid == (gid_t)gid)) {
grp = gr_locate_gid (gid);
Expand Down

0 comments on commit 98aefe8

Please sign in to comment.