Fixed a memory corruption issue, and an erroneous free.

Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>

On Thu, 2008-09-11 at 11:12 -0600, Abhishek Kulkarni wrote:
> Lucho, thanks for the review. 
> 
> On Wed, Sep 10, 2008 at 3:59 PM, Latchesar Ionkov <[EMAIL PROTECTED]>
> wrote:
>         
>         I don't like the fact that there is a maximum number of
>         entries allowed in a conf file. I am not sure what the format
>         of ld.so.conf file is, but I would guess that it allows #
>         comments at the end of lines even if they specify a library
>         path.
> 
> There is no limit on the maximum number of entries now. You are right,
> ldconfig allows # comments at the end of the line too. That should
> work now.
>  
>         
>         
>         It looks like it is possible to write over the last element of
>         the ret array (if (n>maxentries) breaks the loop and then
>         ret[n] = NULL writes outside of its boundaries).
>         
>         I don't think it is a good idea of having FILE * passed to a
>         function that at the end closes the file. Who opens the file
>         should take care of closing it.
> 
> Fixed. I also removed the _GNU_SOURCE define here. 
> 
> Thanks,
>  -- Abhishek
> 
> 
>         
>         
>         Thanks,
>                Lucho
>         
>         
>         
>         On Sep 10, 2008, at 3:02 PM, Abhishek Kulkarni wrote:
>         
>         
>                 
>                 xp_ldd can now read from ld.so.conf for additional
>                 library paths.
>                 It also supports the new ld.so.conf file format with
>                 the 'include'
>                 directive.
>                 
>                 Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>
>                 
>                 <all.patch>
>         
> 
> 
Index: ldd.c
===================================================================
--- ldd.c	(revision 693)
+++ ldd.c	(working copy)
@@ -42,12 +42,16 @@
 #include <errno.h>
 #include <libelf.h>
 #include <gelf.h>
+#include <limits.h>
+#include <glob.h>
 #include "spfs.h"
 #include "spclient.h"
 #include "libxauth.h"
 #include "libxcpu.h"
 #include "xcpuimpl.h"
 
+#define LD_SO_CONF "/etc/ld.so.conf"
+
 int xp_ldd_support = 1;
 
 typedef struct Dep Dep;
@@ -233,6 +237,101 @@
 }
 
 static char **
+parse_conf(char *file)
+{
+	char *line = NULL, *end = NULL;
+	char **ret = NULL, **temp;
+	FILE *fp;
+
+	int n = 0, i, result;
+	glob_t libglob;
+
+	line = sp_malloc(PATH_MAX);
+	if(!line)
+		return NULL;
+
+	if ((fp = fopen(file, "r")) == NULL)
+		return NULL;
+
+	while(fgets(line, PATH_MAX, fp)) {
+
+		/* Skip the initial whitespace */
+		while(*line==' ' || *line=='\t')
+			line++;
+
+		/* Strip comments */
+		if ((end = strchr(line, '#')) != NULL)
+			*end = '\0';
+
+		/* Strip newline character */
+		if ((end = strchr(line, '\n')) != NULL)
+			*end = '\0';
+
+		if (*line == '\0')
+			continue;
+
+		/* Strip trailing whitespaces and tabs */
+		end = line + strlen(line) - 1;
+		while(*end == ' ' || *end == '\t')
+			end--;
+
+		*(end+1) = '\0';
+
+		if (!strncmp(line, "include" , 7)) {
+			line += 8;
+			result = glob(line, GLOB_NOSORT, NULL, &libglob);
+
+			switch(result) {
+			case 0:
+				for(i = 0; i < libglob.gl_pathc; i++) {
+					if ((temp = parse_conf(libglob.gl_pathv[i]))) {
+						while(*temp != NULL) {
+							ret = realloc(ret, (n+1)*sizeof(char *));
+							if (!ret)
+								return NULL;
+
+							ret[n] = sp_malloc(PATH_MAX);
+							strcpy(ret[n], *temp);
+							n++;
+							temp++;
+						}
+					}
+				}
+
+				globfree(&libglob);
+				break;
+
+			case GLOB_NOMATCH:
+				break;
+
+			case GLOB_NOSPACE:
+				sp_werror(Enomem, EIO, line);
+				break;
+
+			case GLOB_ABORTED:
+				sp_werror("%s: Cannot read directory: %s\n", EIO, file, line);
+				break;
+			}
+
+		} else {
+			ret = realloc(ret, (n+1)*sizeof(char *));
+			if (!ret)
+				return NULL;
+			ret[n] = sp_malloc(PATH_MAX);
+			strcpy(ret[n], line);
+			n++;
+		}
+
+	}
+
+	fclose(fp);
+
+	ret = realloc(ret, (n+1)*sizeof(char *));
+	ret[n] = NULL;
+	return ret;
+}
+
+static char **
 parse_path(char *path)
 {
 	int n;
@@ -362,7 +461,7 @@
 		dl->ldlibpath = parse_path(s);
 
 	/* ld.so.conf */
-	// TODO
+	dl->ldsoconf = parse_conf(LD_SO_CONF);
 }
 
 int
@@ -448,7 +547,9 @@
 	}
 
 	free(dl.interp);
-	free(dl.ldsoconf);
+
+	for(i=0; dl.ldsoconf[i] != NULL; i++)
+		free(dl.ldsoconf[i]);
 	free(dl.ldlibpath);
 	return n;
 }

Reply via email to