Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: sh emulation POSIX non-conformances ("inf"/"Inf" in arithmetic expressions)



On 2021-04-26 12:34:36 +0200, Vincent Lefevre wrote:
> On 2021-04-25 15:00:03 -0700, Bart Schaefer wrote:
> > On Sun, Apr 25, 2021 at 1:18 PM Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
> > >
> > > math.c is obviously incorrect:
> > >
> > >                     else if (strncasecmp(p, "Inf", 3) == 0) {
> > 
> > What should it (and the corresponding NaN line) be, then?
> 
> This depends on the expected behavior: Should it match only
> [Ii][Nn][Ff], or also the lowercase and uppercase versions?
> ... so that in Turkish locales under Debian: [Iıİi][Nn][Ff],
> i.e. also with
>   U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE
>   U+0131 LATIN SMALL LETTER DOTLESS I

I think that matching ASCII only would be the expected behavior,
as this would not depend on the locales and this would be like
ksh93, strtod(), and so on. See attached patch. Alternatively,
you could keep the strncasecmp for NaN and possibly for the "nf"
part of "Inf" since AFAIK, 'i' is the only letter to have such
an issue, but for 3 characters, individual comparisons seem OK.

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
diff --git a/Src/math.c b/Src/math.c
index 1d0d86639..ade02d80c 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -864,13 +864,17 @@ zzlex(void)
 		p = ptr;
 		ptr = ie;
 		if (ie - p == 3) {
-		    if (strncasecmp(p, "NaN", 3) == 0) {
+		    if ((p[0] == 'N' || p[0] == 'n') &&
+			(p[1] == 'A' || p[1] == 'a') &&
+			(p[2] == 'N' || p[2] == 'n')) {
 			yyval.type = MN_FLOAT;
 			yyval.u.d = 0.0;
 			yyval.u.d /= yyval.u.d;
 			return NUM;
 		    }
-		    else if (strncasecmp(p, "Inf", 3) == 0) {
+		    else if ((p[0] == 'I' || p[0] == 'i') &&
+			     (p[1] == 'N' || p[1] == 'n') &&
+			     (p[2] == 'F' || p[2] == 'f')) {
 			yyval.type = MN_FLOAT;
 			yyval.u.d = 0.0;
 			yyval.u.d = 1.0 / yyval.u.d;


Messages sorted by: Reverse Date, Date, Thread, Author