[PATCH] fix "Illegal number" on FreeBSD & macOS for x=; echo $((x))

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



dash compiled on Mac OS X (macOS) and FreeBSD manifests the following bug:

$ dash -c 'x=; echo $((x))'
dash: 1: Illegal number:

This error is printed instead of the expected default "0" that should be
substituted for an empty variable in an arithmetic expression.

The bug does not occur on Linux, NetBSD, OpenBSD or Solaris.

I traced the problem to the fact that strtoimax(3) on macOS and FreeBSD
returns EINVAL in errno for an empty string -- unlike those other
systems, which return 0 in errno for an empty string.

POSIX says of strtoimax(3):
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoimax.html
| These functions may fail if:
|
| [EINVAL]
|     No conversion could be performed.

It seems reasonable to consider that "no conversion could be performed"
if the string to convert is empty. Returning EINVAL if no conversion
could be performed is optional ("may fail"). So it seems to me that both
the FreeBSD/macOS behaviour and that of the other systems is POSIX
compliant.

The following patch should eliminate the bug on FreeBSD, macOS and any
other POSIX system which may act the same, without affecting behaviour
on other systems.

- M.

diff --git a/src/mystring.c b/src/mystring.c
index 0106bd2..c7df41f 100644
--- a/src/mystring.c
+++ b/src/mystring.c
@@ -125,7 +125,7 @@ intmax_t atomax(const char *s, int base)
 	errno = 0;
 	r = strtoimax(s, &p, base);

-	if (errno != 0)
+	if (errno != 0 && !(errno == EINVAL && p == s))
 		badnum(s);

 	/*
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux