This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: patch: Make sort-test handle more then 100 lines, and avoid segfault
- From: Petter Reinholdtsen <pere at hungry dot com>
- To: libc-alpha at sources dot redhat dot com
- Date: 10 Jul 2003 09:59:16 +0200
- Subject: Re: patch: Make sort-test handle more then 100 lines, and avoid segfault
- References: <200307100607.h6A67FY14390@magilla.sf.frob.com>
- Reply-by: Tue, 1 Jan 1801 04:37:40 +1000
[Roland McGrath]
> Put space before paren in function calls. Use the `error' function
> instead of fprintf plus exit. Don't cast the arguments to `free'.
> (And when you do need to write one, put a space before a *.) Can
> you send a corrected patch?
Sure. The casting is needed to avoid a compile warning. Is this
version better? The usage message became a bit strange, but I ignore
it as this is a internal test program for glibc.
2003-07-10 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 10 Jul 2003 07:55:43 -0000
@@ -43,6 +43,9 @@ main (int argc, char *argv[])
size_t len = 0;
size_t n;
+ if (argc < 2)
+ error (1, 0, "usage: %s <random seed>", argv[0]);
+
setlocale (LC_ALL, "");
nstrings_max = 100;
@@ -63,8 +66,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 +81,8 @@ main (int argc, char *argv[])
strings[nstrings].key = strndup (line, l);
++nstrings;
}
+ free (line);
+ line = NULL;
/* First shuffle. */
srandom (atoi (argv[1]));
@@ -105,7 +110,13 @@ main (int argc, char *argv[])
/* Print the result. */
for (n = 0; n < nstrings; ++n)
- fputs (strings[n].line, stdout);
+ {
+ fputs (strings[n].line, stdout);
+ /* need to cast 'const' pointer to avoid warning */
+ 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 10 Jul 2003 07:55:43 -0000
@@ -43,6 +43,9 @@ main (int argc, char *argv[])
size_t len = 0;
size_t n;
+ if (argc < 2)
+ error (1, 0, "usage: %s <random seed>", argv[0]);
+
setlocale (LC_ALL, "");
nstrings_max = 100;
@@ -65,8 +68,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 +90,8 @@ main (int argc, char *argv[])
line[l] = saved;
++nstrings;
}
+ free (line);
+ line = NULL;
/* First shuffle. */
srandom (atoi (argv[1]));
@@ -116,7 +121,13 @@ main (int argc, char *argv[])
/* Print the result. */
for (n = 0; n < nstrings; ++n)
- fputs (strings[n].line, stdout);
+ {
+ fputs (strings[n].line, stdout);
+ /* need to cast 'const' pointer to avoid warning */
+ free ((void *)strings[n].line);
+ free ((void *)strings[n].xfrm);
+ }
+ free (strings);
return result;
}