2010-01-20 Felix Zielcke <fzielcke@z-51.de>

* util/misc.c (make_system_path_relative_to_its_root): Change the work
	around for handling "/" to the correct fix.  Fix a memory leak.  Use
	xstrdup instead of strdup.
This commit is contained in:
Felix Zielcke 2010-01-20 23:53:53 +01:00
parent a9ed4ff36f
commit 67eb14272d
2 changed files with 24 additions and 13 deletions

View file

@ -1,3 +1,9 @@
2010-01-20 Felix Zielcke <fzielcke@z-51.de>
* util/misc.c (make_system_path_relative_to_its_root): Change the work
around for handling "/" to the correct fix. Fix a memory leak. Use
xstrdup instead of strdup.
2010-01-20 Vladimir Serbinenko <phcoder@gmail.com> 2010-01-20 Vladimir Serbinenko <phcoder@gmail.com>
* conf/mips.rmk (kernel_img_HEADERS): Add env_private.h * conf/mips.rmk (kernel_img_HEADERS): Add env_private.h

View file

@ -513,13 +513,13 @@ make_system_path_relative_to_its_root (const char *path)
grub_util_error ("failed to get canonical path of %s", path); grub_util_error ("failed to get canonical path of %s", path);
len = strlen (p) + 1; len = strlen (p) + 1;
buf = strdup (p); buf = xstrdup (p);
free (p); free (p);
if (stat (buf, &st) < 0) if (stat (buf, &st) < 0)
grub_util_error ("cannot stat %s: %s", buf, strerror (errno)); grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
buf2 = strdup (buf); buf2 = xstrdup (buf);
num = st.st_dev; num = st.st_dev;
/* This loop sets offset to the number of chars of the root /* This loop sets offset to the number of chars of the root
@ -541,12 +541,16 @@ make_system_path_relative_to_its_root (const char *path)
/* buf is another filesystem; we found it. */ /* buf is another filesystem; we found it. */
if (st.st_dev != num) if (st.st_dev != num)
{ {
/* offset == 0 means path given is the mount point. */ /* offset == 0 means path given is the mount point.
This works around special-casing of "/" in Un*x. This function never
prints trailing slashes (so that its output can be appended a slash
unconditionally). Each slash in is considered a preceding slash, and
therefore the root directory is an empty string. */
if (offset == 0) if (offset == 0)
{ {
free (buf); free (buf);
free (buf2); free (buf2);
return strdup ("/"); return xstrdup ("");
} }
else else
break; break;
@ -563,11 +567,19 @@ make_system_path_relative_to_its_root (const char *path)
buf2[len - 1] = '\0'; buf2[len - 1] = '\0';
len--; len--;
} }
return buf2; if (len > 1)
return buf2;
else
{
/* This means path given is just a backslash. As above
we have to return an empty string. */
free (buf2);
return xtrdup ("");
}
} }
} }
free (buf); free (buf);
buf3 = strdup (buf2 + offset); buf3 = xstrdup (buf2 + offset);
free (buf2); free (buf2);
len = strlen (buf3); len = strlen (buf3);
@ -577,13 +589,6 @@ make_system_path_relative_to_its_root (const char *path)
len--; len--;
} }
/* This works around special-casing of "/" in Un*x. This function never
prints trailing slashes (so that its output can be appended a slash
unconditionally). Each slash in is considered a preceding slash, and
therefore the root directory is an empty string. */
if (!strcmp (buf3, "/"))
buf3[0] = '\0';
return buf3; return buf3;
} }