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: 13 Jul 2003 23:01:31 +0200
- Subject: Re: patch: Make sort-test handle more then 100 lines, and avoid segfault
- References: <200307100904.h6A94w815300@magilla.sf.frob.com>
- Reply-by: Tue, 1 Jan 1801 04:37:40 +1000
Roland McGrath <roland@redhat.com> writes:
> > Sure. The casting is needed to avoid a compile warning.
>
> The correct fix for that is to remove `const' from the types in the struct.
Right. That is probably a better idea. Here is an updated patch.
Sorry that it took so long, but I was (and am) busy organizing
<URL:http://www.debconf.org/debconf3/>. :)
2003-07-13 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 13 Jul 2003 20:57:58 -0000
@@ -27,8 +27,8 @@
struct lines
{
- const char *key;
- const char *line;
+ char *key;
+ char *line;
};
static int xstrcoll (const void *, const void *);
@@ -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,12 @@ main (int argc, char *argv[])
/* Print the result. */
for (n = 0; n < nstrings; ++n)
- fputs (strings[n].line, stdout);
+ {
+ fputs (strings[n].line, stdout);
+ free (strings[n].line);
+ free (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 13 Jul 2003 20:57:58 -0000
@@ -27,8 +27,8 @@
struct lines
{
- const char *xfrm;
- const char *line;
+ char *xfrm;
+ char *line;
};
static int xstrcmp (const void *, const void *);
@@ -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,12 @@ main (int argc, char *argv[])
/* Print the result. */
for (n = 0; n < nstrings; ++n)
- fputs (strings[n].line, stdout);
+ {
+ fputs (strings[n].line, stdout);
+ free (strings[n].line);
+ free (strings[n].xfrm);
+ }
+ free (strings);
return result;
}