[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: a64l (and l64a) implementation is INCORRECT?
From: |
Andreas Schwab |
Subject: |
Re: a64l (and l64a) implementation is INCORRECT? |
Date: |
23 Oct 2001 13:59:02 +0200 |
User-agent: |
Gnus/5.090003 (Oort Gnus v0.03) Emacs/21.1 |
Masaki Hasegawa <address@hidden> writes:
|> Dear Project members
|>
|> Few years ago, I wrote a little program using a64l(3) on HP-UX machine.
|> And I have tried to port it to my Linux box that is running with glibc
|> 2.2.2, I have found that glibc's a64l returns differnt value than HP's.
|> For example:
|> on HP machine ... a64l("./") returns 64 , but
|> on glibc ... a64l("./") returns 1 .
|>
|> According to the Open Group "The Single UNIX Specification, Version 2"
|> (http://www.opengroup.org/onlinepubs/007908799/xsh/a64l.html):
|> > The a64l() function takes a pointer to a radix-64 representation,
|> > in which the first digit is the least significant, and returns ...
|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|> It seems that the glibc's implementation is INCORRECT.
Thanks for your report. The following patch will fix the problem:
2001-10-23 Andreas Schwab <address@hidden>
* stdlib/a64l.c: Expect least significant digit first.
* stdlib/l64a.c: Produce least significant digit first.
* stdlib/Makefile (tests): Add test-a64l.
* stdlib/test-a64l.c: New file.
Reported by Masaki Hasegawa <address@hidden>.
Index: stdlib/Makefile
===================================================================
RCS file: /cvs/glibc/libc/stdlib/Makefile,v
retrieving revision 1.78
diff -u -a -r1.78 stdlib/Makefile
--- stdlib/Makefile 2001/09/12 18:48:01 1.78
+++ stdlib/Makefile 2001/10/23 11:53:26
@@ -59,7 +59,7 @@
tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
test-canon test-canon2 tst-strtoll tst-environ \
tst-xpg-basename tst-random tst-bsearch tst-limits \
- tst-rand48 bug-strtod tst-setcontext
+ tst-rand48 bug-strtod tst-setcontext test-a64l
# Several mpn functions from GNU MP are used by the strtod function.
Index: stdlib/a64l.c
===================================================================
RCS file: /cvs/glibc/libc/stdlib/a64l.c,v
retrieving revision 1.7
diff -u -a -r1.7 stdlib/a64l.c
--- stdlib/a64l.c 2001/07/06 04:55:41 1.7
+++ stdlib/a64l.c 2001/10/23 11:53:26
@@ -43,6 +43,7 @@
const char *ptr = string;
unsigned long int result = 0ul;
const char *end = ptr + 6;
+ int shift = 0;
do
{
@@ -55,9 +56,9 @@
value = (int) a64l_table[index];
if (value == (int) XX)
break;
- result <<= 6;
++ptr;
- result |= value;
+ result |= value << shift;
+ shift += 6;
}
while (ptr != end);
Index: stdlib/l64a.c
===================================================================
RCS file: /cvs/glibc/libc/stdlib/l64a.c,v
retrieving revision 1.8
diff -u -a -r1.8 stdlib/l64a.c
--- stdlib/l64a.c 2001/07/06 04:55:41 1.8
+++ stdlib/l64a.c 2001/10/23 11:53:26
@@ -47,13 +47,12 @@
/* The value for N == 0 is defined to be the empty string. */
return (char *) "";
- result[6] = '\0';
-
- for (cnt = 5; m > 0ul; --cnt)
+ for (cnt = 0; m > 0ul; ++cnt)
{
result[cnt] = conv_table[m & 0x3f];
m >>= 6;
}
+ result[cnt] = '\0';
- return &result[cnt + 1];
+ return result;
}
Index: stdlib/test-a64l.c
===================================================================
RCS file: stdlib/test-a64l.c
diff -N stdlib/test-a64l.c
--- /dev/null Tue May 5 13:32:27 1998
+++ stdlib/test-a64l.c Tue Oct 23 04:53:26 2001
@@ -0,0 +1,75 @@
+/* Test program for the l64a and a64l functions.
+ Copyright (C) 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Andreas Schwab <address@hidden>.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Prototype for our test function. */
+extern int do_test (int argc, char *argv[]);
+#include <test-skeleton.c>
+
+struct a64l_test
+{
+ const char *base64;
+ long int value;
+};
+
+static const struct a64l_test tests[] =
+ {
+ { "./", 64 },
+ { "", 0 },
+ { "/", 1 },
+ { "FT", 2001 },
+ { NULL, 0 }
+ };
+
+int
+do_test (int argc, char ** argv)
+{
+ const struct a64l_test *at;
+ long int l;
+ const char *s;
+ int status = 0;
+
+ for (at = tests; at->base64 != NULL; ++at)
+ {
+ printf ("a64l (\"%s\")", at->base64);
+ l = a64l (at->base64);
+ if (l == at->value)
+ puts ("\tOK");
+ else
+ {
+ printf ("\tBAD\n returns %ld, expected %ld\n", l, at->value);
+ status = 1;
+ }
+ printf ("l64a (%ld)", at->value);
+ s = l64a (at->value);
+ if (strcmp (s, at->base64) == 0)
+ puts ("\tOK");
+ else
+ {
+ printf ("\tBAD\n returns \"%s\", expected \"%s\"\n", s, at->base64);
+ status = 1;
+ }
+ }
+
+ return status ? EXIT_FAILURE : EXIT_SUCCESS;
+}
--
Andreas Schwab "And now for something
address@hidden completely different."
SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5