From: Joerg Sonnenberger Date: Sun, 18 Dec 2016 10:50:10 +0000 (+0100) Subject: Allocate memory precisely. Avoid recomputions of sizes. X-Git-Tag: v3.3.0~82 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f6565656cf20c74057289d784f548ffd189b416;p=thirdparty%2Flibarchive.git Allocate memory precisely. Avoid recomputions of sizes. Based on Coverity report from pull request #844. --- diff --git a/tar/read.c b/tar/read.c index e94cb3da8..3c6cb0c13 100644 --- a/tar/read.c +++ b/tar/read.c @@ -188,18 +188,17 @@ read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer) reader_options = getenv(ENV_READER_OPTIONS); if (reader_options != NULL) { + size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1; + size_t opt_len = strlen(reader_options) + 1; char *p; /* Set default read options. */ - p = (char *)malloc(sizeof(IGNORE_WRONG_MODULE_NAME) - + strlen(reader_options) + 1); - if (p == NULL) + if ((p = malloc(module_len + opt_len)) == NULL) lafe_errc(1, errno, "Out of memory"); /* Prepend magic code to ignore options for * a format or modules which are not added to * the archive read object. */ - strncpy(p, IGNORE_WRONG_MODULE_NAME, - sizeof(IGNORE_WRONG_MODULE_NAME) -1); - strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, reader_options); + memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len); + memcpy(p + module_len, reader_options, opt_len); r = archive_read_set_options(a, p); free(p); if (r == ARCHIVE_FATAL)