I: [PATCH] realloc-related memory leaks fixes

Dmitry V. Levin ldv@alt-linux.org
Fri Dec 28 13:18:00 GMT 2001


On Fri, Dec 28, 2001 at 05:56:56PM +0100, Andreas Jaeger wrote:
> > - resolv/res_hconf.c seems to have memory leak (possible fix also
> >   attached);
> 
> I don't see a memory leak in res_hconf. Please explain what's wrong.  The
> function is called only once per program.

In this case my patch isn't really useful.

> > - io/fts.c contains memory leaks; I'd suggest to update code from BSD; for
> >   example, OpenBSD have these bugs fixed.
> 
> Can you send a patch for fts.c, please?

Sure.
The patch contains update only for realloc-related functions.


Regards,
	Dmitry

+-------------------------------------------------------------------------+
Dmitry V. Levin     mailto://ldv@alt-linux.org
ALT Linux Team      http://www.altlinux.ru/
Fandra Project      http://www.fandra.org/
+-------------------------------------------------------------------------+
UNIX is user friendly. It's just very selective about who its friends are.
-------------- next part --------------
2001-12-25  Dmitry V. Levin <ldv@alt-linux.org>

	* io/fts.c: Update from BSD to fix memory leaks.
	
--- glibc-20011223~/io/fts.c	Mon Aug 20 10:59:41 2001
+++ glibc-20011223/io/fts.c	Fri Dec 25 21:31:50 2001
@@ -934,12 +934,19 @@
 	 * 40 so don't realloc one entry at a time.
 	 */
 	if (nitems > sp->fts_nitems) {
+		struct _ftsent **a;
+
 		sp->fts_nitems = nitems + 40;
-		if ((sp->fts_array = realloc(sp->fts_array,
+		if ((a = realloc(sp->fts_array,
 		    (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
+			if (sp->fts_array != NULL) {
+				free(sp->fts_array);
+				sp->fts_array = NULL;
+			}
 			sp->fts_nitems = 0;
 			return (head);
 		}
+		sp->fts_array = a;
 	}
 	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
 		*ap++ = p;
@@ -1016,6 +1023,8 @@
 	FTS *sp;
 	size_t more;
 {
+	char *p;
+
 	sp->fts_pathlen += more + 256;
 	/*
 	 * Check for possible wraparound.  In an FTS, fts_pathlen is
@@ -1023,14 +1032,23 @@
 	 * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
 	 */
 	if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) {
-		if (sp->fts_path)
+		if (sp->fts_path) {
 			free(sp->fts_path);
-		sp->fts_path = NULL;
+			sp->fts_path = NULL;
+		}
 		__set_errno (ENAMETOOLONG);
 		return (1);
 	}
-	sp->fts_path = realloc(sp->fts_path, sp->fts_pathlen);
-	return (sp->fts_path == NULL);
+	p = realloc(sp->fts_path, sp->fts_pathlen);
+	if (p == NULL) {
+		if (sp->fts_path) {
+			free(sp->fts_path);
+			sp->fts_path = NULL;
+		}
+		return 1;
+	}
+	sp->fts_path = p;
+	return 0;
 }
 
 /*
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20011228/7820afa1/attachment.sig>


More information about the Libc-alpha mailing list