From 61e1b9a49d48035bde52784abb54c3212b647fc8 Mon Sep 17 00:00:00 2001 From: Vladimir Serbinenko Date: Mon, 25 Nov 2013 07:34:49 +0100 Subject: [PATCH] * grub-core/osdep/unix/platform.c (get_ofpathname): Trim ending newline. Don't rely on PATH_MAX. --- ChangeLog | 5 +++++ grub-core/osdep/unix/platform.c | 28 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6b094216a..75439bbb8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2013-11-25 Vladimir Serbinenko + + * grub-core/osdep/unix/platform.c (get_ofpathname): Trim ending newline. + Don't rely on PATH_MAX. + 2013-11-25 Vladimir Serbinenko * grub-core/genmoddep.awk: Use more portable && rather than and. diff --git a/grub-core/osdep/unix/platform.c b/grub-core/osdep/unix/platform.c index 65c93f150..dc296c98b 100644 --- a/grub-core/osdep/unix/platform.c +++ b/grub-core/osdep/unix/platform.c @@ -31,11 +31,11 @@ static char * get_ofpathname (const char *dev) { - char *ret = xmalloc (2 * PATH_MAX); - char *end = ret + 2 * PATH_MAX - 1; + size_t alloced = 4096; + char *ret = xmalloc (alloced); + size_t offset = 0; int fd; pid_t pid; - char *ptr = ret; pid = grub_util_exec_pipe ((const char * []){ "ofpathname", dev, NULL }, &fd); if (!pid) @@ -45,13 +45,29 @@ get_ofpathname (const char *dev) if (!fp) goto fail; - while (!feof (fp) && ptr < end) + while (!feof (fp)) { size_t r; - r = fread (ptr, 1, end - ptr, fp); - ptr += r; + if (alloced == offset) + { + alloced *= 2; + ret = xrealloc (ret, alloced); + } + r = fread (ret + offset, 1, alloced - offset, fp); + offset += r; } + if (offset > 0 && ret[offset - 1] == '\n') + offset--; + if (offset > 0 && ret[offset - 1] == '\r') + offset--; + if (alloced == offset) + { + alloced++; + ret = xrealloc (ret, alloced); + } + ret[offset] = '\0'; + fclose (fp); return ret;