patch: Make sort-test handle more then 100 lines, and avoid segfault
Petter Reinholdtsen
pere@hungry.com
Sun Jul 6 20:31:00 GMT 2003
The sorting test is at the moment unable to handle tests with more the
100 lines, because of a typo in the calculation of nstrings_max. This
patch move a parentheses to it's correct location. It also add
argument checking, to avoid segfault if the required first parameter
is missing, and free the malloc()ed memory to make it easier to spot
errors using valgrind.
Please include the change in the next version of glibc.
2003-07-06 Petter Reinholdtsen <pere@hungry.com>
* collate-test.c (main): Correct handling of files with more then
100 lines. Print error message if argument is missing, instead of
segfaulting. Avoid memleak in test code.
* xfrm-test.c: Likewise.
Index: localedata/collate-test.c
===================================================================
RCS file: /cvs/glibc/libc/localedata/collate-test.c,v
retrieving revision 1.5
diff -u -3 -p -u -r1.5 collate-test.c
--- localedata/collate-test.c 6 Jul 2001 04:55:34 -0000 1.5
+++ localedata/collate-test.c 6 Jul 2003 20:25:45 -0000
@@ -43,6 +43,12 @@ main (int argc, char *argv[])
size_t len = 0;
size_t n;
+ if (argc < 2)
+ {
+ fprintf(stderr, "usage: %s <random seed>\n", argv[0]);
+ exit (1);
+ }
+
setlocale (LC_ALL, "");
nstrings_max = 100;
@@ -63,8 +69,8 @@ main (int argc, char *argv[])
if (nstrings == nstrings_max)
{
strings = (struct lines *) realloc (strings,
- (nstrings_max *= 2
- * sizeof (*strings)));
+ (nstrings_max *= 2)
+ * sizeof (*strings));
if (strings == NULL)
{
perror (argv[0]);
@@ -78,6 +84,8 @@
strings[nstrings].key = strndup (line, l);
++nstrings;
}
+ free (line);
+ line = NULL;
/* First shuffle. */
srandom (atoi (argv[1]));
@@ -105,7 +113,12 @@
/* Print the result. */
for (n = 0; n < nstrings; ++n)
- fputs (strings[n].line, stdout);
+ {
+ fputs (strings[n].line, stdout);
+ free ((void*)strings[n].line);
+ free ((void*)strings[n].key);
+ }
+ free (strings);
return result;
}
Index: localedata/xfrm-test.c
===================================================================
RCS file: /cvs/glibc/libc/localedata/xfrm-test.c,v
retrieving revision 1.5
diff -u -3 -p -u -r1.5 xfrm-test.c
--- localedata/xfrm-test.c 6 Jul 2001 04:55:34 -0000 1.5
+++ localedata/xfrm-test.c 6 Jul 2003 20:25:45 -0000
@@ -43,6 +43,12 @@ main (int argc, char *argv[])
size_t len = 0;
size_t n;
+ if (argc < 2)
+ {
+ fprintf(stderr, "usage: %s <random seed>\n", argv[0]);
+ exit (1);
+ }
+
setlocale (LC_ALL, "");
nstrings_max = 100;
@@ -65,8 +71,8 @@ main (int argc, char *argv[])
if (nstrings == nstrings_max)
{
strings = (struct lines *) realloc (strings,
- (nstrings_max *= 2
- * sizeof (*strings)));
+ (nstrings_max *= 2)
+ * sizeof (*strings));
if (strings == NULL)
{
perror (argv[0]);
@@ -87,6 +93,8 @@
line[l] = saved;
++nstrings;
}
+ free (line);
+ line = NULL;
/* First shuffle. */
srandom (atoi (argv[1]));
@@ -116,7 +124,12 @@
/* Print the result. */
for (n = 0; n < nstrings; ++n)
- fputs (strings[n].line, stdout);
+ {
+ fputs (strings[n].line, stdout);
+ free ((void*)strings[n].line);
+ free ((void*)strings[n].xfrm);
+ }
+ free (strings);
return result;
}
More information about the Libc-alpha
mailing list