Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove strtou[l]l_noneg(), replacing them by a2i() calls #895

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/atoi/strtou_noneg.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@

extern inline uintmax_t strtou_noneg(const char *s, char **restrict endp,
int base, uintmax_t min, uintmax_t max, int *restrict status);

extern inline unsigned long strtoul_noneg(const char *s,
char **restrict endp, int base);
extern inline unsigned long long strtoull_noneg(const char *s,
char **restrict endp, int base);
31 changes: 1 addition & 30 deletions lib/atoi/strtou_noneg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <config.h>

#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#include "atoi/strtoi.h"
#include "attr.h"
Expand All @@ -20,13 +20,6 @@ ATTR_STRING(1) ATTR_ACCESS(write_only, 2) ATTR_ACCESS(write_only, 6)
inline uintmax_t strtou_noneg(const char *s, char **restrict endp,
int base, uintmax_t min, uintmax_t max, int *restrict status);

ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline unsigned long strtoul_noneg(const char *s,
char **restrict endp, int base);
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline unsigned long long strtoull_noneg(const char *s,
char **restrict endp, int base);


inline uintmax_t
strtou_noneg(const char *s, char **restrict endp, int base,
Expand All @@ -43,26 +36,4 @@ strtou_noneg(const char *s, char **restrict endp, int base,
}


inline unsigned long
strtoul_noneg(const char *s, char **restrict endp, int base)
{
if (strtol(s, endp, base) < 0) {
errno = ERANGE;
return 0;
}
return strtoul(s, endp, base);
}


inline unsigned long long
strtoull_noneg(const char *s, char **restrict endp, int base)
{
if (strtol(s, endp, base) < 0) {
errno = ERANGE;
return 0;
}
return strtoull(s, endp, base);
}


#endif // include guard
56 changes: 17 additions & 39 deletions lib/gettime.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
* SPDX-FileCopyrightText: 2017, Chris Lamb
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2017, Chris Lamb
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

Expand All @@ -12,58 +11,37 @@
#include <limits.h>
#include <stdio.h>

#include "atoi/strtou_noneg.h"
#include "atoi/a2i.h"
#include "defines.h"
#include "prototypes.h"
#include "shadowlog.h"


/*
* gettime() returns the time as the number of seconds since the Epoch
*
* Like time(), gettime() returns the time as the number of seconds since the
* Epoch, 1970-01-01 00:00:00 +0000 (UTC), except that if the SOURCE_DATE_EPOCH
* environment variable is exported it will use that instead.
*/
/*@observer@*/time_t gettime (void)
/*@observer@*/time_t
gettime(void)
{
char *end;
char *source_date_epoch;
time_t fallback;
unsigned long long epoch;
FILE *shadow_logfd = log_get_logfd();
char *source_date_epoch;
FILE *shadow_logfd = log_get_logfd();
time_t fallback, epoch;

fallback = time (NULL);
source_date_epoch = shadow_getenv ("SOURCE_DATE_EPOCH");

if (!source_date_epoch)
return fallback;

errno = 0;
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 (end == source_date_epoch) {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n"),
end);
} else if (*end != '\0') {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n"),
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"),
ULONG_MAX, epoch);
} else if ((time_t)epoch > fallback) {
fprintf (shadow_logfd,
_("Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to the current time (%lu) but was found to be: %llu\n"),
fallback, epoch);
} else {
/* Valid */
return epoch;
if (a2i(time_t, &epoch, source_date_epoch, NULL, 10, 0, fallback) == -1) {
fprintf(shadow_logfd,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lamby Should we accept negative values? Why did this use strtoull(1) originally?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm blocked until this questions is answered. Feel free to close it and merge the PR at your convenience

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm merging.

ikerexxe marked this conversation as resolved.
Show resolved Hide resolved
_("Environment variable $SOURCE_DATE_EPOCH: a2i(\"%s\"): %s"),
source_date_epoch, strerror(errno));
return fallback;
}

return fallback;
return epoch;
}
8 changes: 3 additions & 5 deletions src/check_subid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <sys/stat.h>
#include <fcntl.h>

#include "atoi/strtou_noneg.h"
#include "atoi/str2i.h"
#include "defines.h"
#include "prototypes.h"
#include "subordinateio.h"
Expand All @@ -36,11 +36,9 @@ int main(int argc, char **argv)
owner = argv[1];
check_uids = argv[2][0] == 'u';
errno = 0;
start = strtoul_noneg(argv[3], NULL, 10);
if (errno != 0)
if (str2ul(&start, argv[3]) == -1)
ikerexxe marked this conversation as resolved.
Show resolved Hide resolved
exit(1);
count = strtoul_noneg(argv[4], NULL, 10);
if (errno != 0)
if (str2ul(&count, argv[4]) == -1)
exit(1);
if (check_uids) {
if (have_sub_uids(owner, start, count))
Expand Down
14 changes: 0 additions & 14 deletions tests/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ TESTS = $(check_PROGRAMS)
check_PROGRAMS = \
test_adds \
test_atoi_strtoi \
test_atoi_strtou_noneg \
test_chkname \
test_sprintf \
test_strncpy \
Expand Down Expand Up @@ -48,19 +47,6 @@ test_atoi_strtoi_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)

test_atoi_strtou_noneg_SOURCES = \
../../lib/atoi/strtou_noneg.c \
test_atoi_strtou_noneg.c \
$(NULL)
test_atoi_strtou_noneg_CFLAGS = \
$(AM_CFLAGS) \
$(NULL)
test_atoi_strtou_noneg_LDFLAGS = \
$(NULL)
test_atoi_strtou_noneg_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)

test_chkname_SOURCES = \
../../lib/chkname.c \
test_chkname.c \
Expand Down
76 changes: 0 additions & 76 deletions tests/unit/test_atoi_strtou_noneg.c

This file was deleted.