summaryrefslogtreecommitdiff
path: root/tar
diff options
context:
space:
mode:
authorJoerg Sonnenberger <joerg@bec.de>2016-12-18 11:55:30 +0100
committerJoerg Sonnenberger <joerg@bec.de>2016-12-18 11:55:30 +0100
commitd7e5cbde8754a0bb078d683c7906630a76a13832 (patch)
tree1351b18cbdb6b4e289454a784da21888548085ab /tar
parent7f6565656cf20c74057289d784f548ffd189b416 (diff)
downloadlibarchive-d7e5cbde8754a0bb078d683c7906630a76a13832.tar.gz
Compute correct size for buffer. Avoid recomputation of lengths.
Based on Coverity reports from pull request #844.
Diffstat (limited to 'tar')
-rw-r--r--tar/write.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/tar/write.c b/tar/write.c
index 7960487f..fb8bb92b 100644
--- a/tar/write.c
+++ b/tar/write.c
@@ -145,18 +145,17 @@ set_writer_options(struct bsdtar *bsdtar, struct archive *a)
writer_options = getenv(ENV_WRITER_OPTIONS);
if (writer_options != NULL) {
+ size_t module_len = sizeof(IGNORE_WRONG_MODULE_NAME) - 1;
+ size_t opt_len = strlen(writer_options) + 1;
char *p;
/* Set default write options. */
- p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME)
- + strlen(writer_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 filters which are not added to
* the archive write object. */
- strncpy(p, IGNORE_WRONG_MODULE_NAME,
- sizeof(IGNORE_WRONG_MODULE_NAME) -1);
- strcpy(p + sizeof(IGNORE_WRONG_MODULE_NAME) -1, writer_options);
+ memcpy(p, IGNORE_WRONG_MODULE_NAME, module_len);
+ memcpy(p, writer_options, opt_len);
r = archive_write_set_options(a, p);
free(p);
if (r < ARCHIVE_WARN)
@@ -178,18 +177,18 @@ set_reader_options(struct bsdtar *bsdtar, struct archive *a)
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 write options. */
- p = malloc(sizeof(IGNORE_WRONG_MODULE_NAME)
- + strlen(reader_options) + 1);
+ if ((p = malloc(module_len + opt_len)) == NULL)
if (p == NULL)
lafe_errc(1, errno, "Out of memory");
/* Prepend magic code to ignore options for
* a format or filters which are not added to
* the archive write 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, reader_options, opt_len);
r = archive_read_set_options(a, p);
free(p);
if (r < ARCHIVE_WARN)