Pretty easy to implement string-to-integer conversion in the tests
now that we have some helpers to do so that can be shared with the
main library code.
There is very little advantage to this change in the test programs,
as the reduced-range fallback is pretty much fine here. However,
as there are no other users, getting rid of this also allows us to
remove the configure probes for strtoumax and its alternatives.
AC_CONFIG_TESTDIR([.], [t:.])
DX_PROG_AUTOTEST_AM
AC_CONFIG_TESTDIR([.], [t:.])
DX_PROG_AUTOTEST_AM
-AC_CHECK_FUNCS_ONCE([strtoumax strtoull __strtoull])
-AC_CONFIG_FILES([
- Makefile
-])
+AC_CONFIG_FILES([Makefile])
- if (count_str && !strict_strtoul(&count, count_str, 0)) {
+ if (count_str && !test_strtoul(&count, count_str)) {
fprintf(stderr, "%s: invalid count: %s\n", progname, count_str);
return EXIT_FAILURE;
}
fprintf(stderr, "%s: invalid count: %s\n", progname, count_str);
return EXIT_FAILURE;
}
mode = MODE_ENGLISH;
break;
case 'n':
mode = MODE_ENGLISH;
break;
case 'n':
- if (!strict_strtoul(&n, optarg, 0) || n > (size_t)-2) {
+ if (!test_strtoul(&n, optarg) || n > (size_t)-2) {
fprintf(stderr, "%s: invalid count: %s\n",
progname, optarg);
return EXIT_FAILURE;
fprintf(stderr, "%s: invalid count: %s\n",
progname, optarg);
return EXIT_FAILURE;
-#if HAVE_STRTOULL
-# define STRTOULL strtoull
-#elif HAVE___STRTOULL
-/* HP-UX 11 has __strtoull in <inttypes.h> */
-# define STRTOULL __strtoull
-#else
-/*
- * Just fall back to strtoul -- in the worst case we just lose the ability
- * to set all 64 bits of the seed.
- */
-# define STRTOULL strtoul
-#endif
-
struct test_rng *test_rng_alloc(const char *seed_str)
{
unsigned long long seed;
struct test_rng *test_rng_alloc(const char *seed_str)
{
unsigned long long seed;
+ uintmax_t limit, seed_val;
- errno = 0;
- seed = STRTOULL(seed_str, &end, 0);
- if (*end != 0) {
+ limit = (uintmax_t)0xffffffff;
+ limit |= (limit << 16 << 16);
+
+ if (!test_strtoumax(&seed_val, seed_str, limit)) {
fprintf(stderr, "%s: invalid seed\n", seed_str);
return NULL;
fprintf(stderr, "%s: invalid seed\n", seed_str);
return NULL;
- } else if (errno != 0) {
- fprintf(stderr, "%s: invalid seed: %s\n",
- seed_str, strerror(errno));
- return NULL;
}
rng = malloc_nofail(sizeof *rng);
}
rng = malloc_nofail(sizeof *rng);
void *malloc_nofail(size_t size);
void *realloc_nofail(void *ptr, size_t size);
void *malloc_nofail(size_t size);
void *realloc_nofail(void *ptr, size_t size);
-bool strict_strtoul(unsigned long *val, const char *str, int base);
+bool test_strtoumax(uintmax_t *out, const char *s, uintmax_t limit);
+
+static inline bool test_strtoul(unsigned long *val, const char *str)
+{
+ uintmax_t v;
+ bool rc;
+
+ rc = test_strtoumax(&v, str, (unsigned long)-1);
+ *val = v;
+ return rc;
+}
void test_print_options(const struct option *lopts);
void test_print_options(const struct option *lopts);
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <cdecl.h>
#include "help.h"
#include "test.h"
#include <errno.h>
#include <getopt.h>
#include <cdecl.h>
#include "help.h"
#include "test.h"
void *realloc_nofail(void *ptr, size_t size)
{
void *realloc_nofail(void *ptr, size_t size)
{
return realloc_nofail(NULL, size);
}
return realloc_nofail(NULL, size);
}
-bool strict_strtoul(unsigned long *val, const char *str, int base)
+static unsigned intconv_base(const char **str)
+ if ((*str)[0] == '0') {
+ if ((*str)[1] == 'X' || (*str)[1] == 'x') {
+ *str += 2;
+ return INTCONV_HEXADECIMAL;
+ }
- errno = 0;
- *val = strtoul(str, &end, base);
- if (errno != 0 || *end != 0)
- return false;
+ return INTCONV_OCTAL;
+ }
+
+ return INTCONV_DECIMAL;
+}
+
+bool test_strtoumax(uintmax_t *out, const char *s, uintmax_t limit)
+{
+ static const char idx[] = "0123456789abcdef0123456789ABCDEF";
+ unsigned base = intconv_base(&s);
+ uintmax_t v;
+ char *c, d;
+
+ for (v = 0; (d = *s++);) {
+ if (!(c = strchr(idx, d)) || (d = (c-idx) & 0xf) >= base)
+ return 0;
+
+ if (!intconv_shift(&v, base, d) || v > limit)
+ return 0;
+ }