When insmod work had moved to module-init-tools, there was some modifications to check for allocation failures and avoid using invalid results. These changes were made in the https://git.kernel.org/pub/scm/utils/kernel/module-init-tools/module-init-tools.git repository. This ports those changes back to klibc. Signed-off-by: Kevin Guthrie <Kevin.Guthrie at itron.com> --- usr/utils/insmod.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/usr/utils/insmod.c b/usr/utils/insmod.c index 47b58800..435fed21 100644 --- a/usr/utils/insmod.c +++ b/usr/utils/insmod.c @@ -58,8 +58,8 @@ static const char *moderror(int err) static void *grab_file(const char *filename, unsigned long *size) { unsigned int max = 16384; - int ret, fd; - void *buffer = malloc(max); + int ret, fd, err_save; + void *buffer; if (streq(filename, "-")) fd = dup(STDIN_FILENO); @@ -69,11 +69,21 @@ static void *grab_file(const char *filename, unsigned long *size) if (fd < 0) return NULL; + buffer = malloc(max); + if (!buffer) + goto out_error; + *size = 0; while ((ret = read(fd, buffer + *size, max - *size)) > 0) { *size += ret; - if (*size == max) - buffer = realloc(buffer, max *= 2); + if (*size == max) { + void* p; + + p = realloc(buffer, max *= 2); + if (!p) + goto out_error; + buffer = p; + } } if (ret < 0) { free(buffer); @@ -81,6 +91,13 @@ static void *grab_file(const char *filename, unsigned long *size) } close(fd); return buffer; + +out_error: + err_save = errno; + free(buffer); + close(fd); + errno = err_save; + return NULL; } int main(int argc, char *argv[]) @@ -113,6 +130,11 @@ int main(int argc, char *argv[]) for (i = 2; i < argc; i++) { options = realloc(options, strlen(options) + 2 + strlen(argv[i]) + 2); + if (!options) { + fprintf(stderr, "insmod: can't allocate memory: %sn", + strerror(errno)); + exit(1); + } /* Spaces handled by "" pairs, but no way of escaping quotes */ if (strchr(argv[i], ' ')) @@ -134,7 +156,10 @@ int main(int argc, char *argv[]) if (ret != 0) { fprintf(stderr, "insmod: error inserting '%s': %li %s\n", filename, ret, moderror(errno)); - exit(1); } + free(file); + + if (ret != 0) + exit(1); exit(0); } -- 2.48.1