From 475e390e80a950a8746dc0b88341742dc99014e4 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Tue, 25 May 1999 22:50:11 +0000 Subject: Update. 1999-05-25 Ulrich Drepper * sysdeps/posix/getcwd.c (__getcwd): Fix potential memory leaks. --- sysdeps/posix/getcwd.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'sysdeps/posix/getcwd.c') diff --git a/sysdeps/posix/getcwd.c b/sysdeps/posix/getcwd.c index df48804f7a..fe094b5a35 100644 --- a/sysdeps/posix/getcwd.c +++ b/sysdeps/posix/getcwd.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc. +/* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -211,8 +211,9 @@ __getcwd (buf, size) = "../../../../../../../../../../../../../../../../../../../../../../../\ ../../../../../../../../../../../../../../../../../../../../../../../../../../\ ../../../../../../../../../../../../../../../../../../../../../../../../../.."; - const char *dotp, *dotlist; - size_t dotsize; + const char *dotp = &dots[sizeof (dots)]; + const char *dotlist = dots; + size_t dotsize = sizeof (dots) - 1; dev_t rootdev, thisdev; ino_t rootino, thisino; char *path; @@ -244,18 +245,15 @@ __getcwd (buf, size) *--pathp = '\0'; if (__lstat (".", &st) < 0) - return NULL; + goto lose2; thisdev = st.st_dev; thisino = st.st_ino; if (__lstat ("/", &st) < 0) - return NULL; + goto lose2; rootdev = st.st_dev; rootino = st.st_ino; - dotsize = sizeof (dots) - 1; - dotp = &dots[sizeof (dots)]; - dotlist = dots; while (!(thisdev == rootdev && thisino == rootino)) { register DIR *dirstream; @@ -273,7 +271,7 @@ __getcwd (buf, size) { new = malloc (dotsize * 2 + 1); if (new == NULL) - return NULL; + goto lose; #ifdef HAVE_MEMPCPY dotp = mempcpy (new, dots, dotsize); #else @@ -375,7 +373,6 @@ __getcwd (buf, size) if (tmp == NULL) { (void) __closedir (dirstream); - free (path); __set_errno (ENOMEM);/* closedir might have changed it.*/ goto lose; } @@ -412,6 +409,9 @@ __getcwd (buf, size) lose: if (dotlist != dots) free ((__ptr_t) dotlist); + lose2: + if (buf == NULL) + free (path); return NULL; } -- cgit 1.4.1