Skip to content

Commit fc11e63

Browse files
authored
Merge pull request libarchive#1744 from seanm/snprintf
Fixed issue libarchive#1743: Changed sprintf to safer snprintf
2 parents 12b9856 + 8ddc25d commit fc11e63

25 files changed

Lines changed: 104 additions & 103 deletions

libarchive/archive_read_support_format_7zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ archive_read_format_7zip_read_header(struct archive_read *a,
776776
}
777777

778778
/* Set up a more descriptive format name. */
779-
sprintf(zip->format_name, "7-Zip");
779+
snprintf(zip->format_name, sizeof(zip->format_name), "7-Zip");
780780
a->archive.archive_format_name = zip->format_name;
781781

782782
return (ret);

libarchive/archive_read_support_format_cab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ archive_read_format_cab_read_header(struct archive_read *a,
996996
cab->end_of_entry_cleanup = cab->end_of_entry = 1;
997997

998998
/* Set up a more descriptive format name. */
999-
sprintf(cab->format_name, "CAB %d.%d (%s)",
999+
snprintf(cab->format_name, sizeof(cab->format_name), "CAB %d.%d (%s)",
10001000
hd->major, hd->minor, cab->entry_cffolder->compname);
10011001
a->archive.archive_format_name = cab->format_name;
10021002

libarchive/archive_read_support_format_lha.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ archive_read_format_lha_read_header(struct archive_read *a,
739739
if (lha->directory || lha->compsize == 0)
740740
lha->end_of_entry = 1;
741741

742-
sprintf(lha->format_name, "lha -%c%c%c-",
742+
snprintf(lha->format_name, sizeof(lha->format_name), "lha -%c%c%c-",
743743
lha->method[0], lha->method[1], lha->method[2]);
744744
a->archive.archive_format_name = lha->format_name;
745745

libarchive/archive_write_set_format_pax.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ build_pax_attribute_name(char *dest, const char *src)
17171717
* to having clients override it.
17181718
*/
17191719
#if HAVE_GETPID && 0 /* Disable this for now; see above comment. */
1720-
sprintf(buff, "PaxHeader.%d", getpid());
1720+
snprintf(buff, sizeof(buff), "PaxHeader.%d", getpid());
17211721
#else
17221722
/* If the platform can't fetch the pid, don't include it. */
17231723
strcpy(buff, "PaxHeader");

libarchive/test/test_acl_platform_nfs4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ DEFINE_TEST(test_acl_platform_nfs4)
907907
assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
908908

909909
for (i = 0; i < acls_dir_cnt; ++i) {
910-
sprintf(buff, "dir%d", i);
910+
snprintf(buff, sizeof(buff), "dir%d", i);
911911
archive_entry_set_pathname(ae, buff);
912912
archive_entry_set_filetype(ae, AE_IFDIR);
913913
archive_entry_set_perm(ae, 0654);
@@ -960,7 +960,7 @@ DEFINE_TEST(test_acl_platform_nfs4)
960960

961961
/* Verify single-permission dirs on disk. */
962962
for (i = 0; i < dircnt; ++i) {
963-
sprintf(buff, "dir%d", i);
963+
snprintf(buff, sizeof(buff), "dir%d", i);
964964
assertEqualInt(0, stat(buff, &st));
965965
assertEqualInt(st.st_mtime, 123456 + i);
966966
#if ARCHIVE_ACL_SUNOS_NFS4

libarchive/test/test_archive_api_feature.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DEFINE_TEST(test_archive_api_feature)
3232

3333
/* This is the (hopefully) final versioning API. */
3434
assertEqualInt(ARCHIVE_VERSION_NUMBER, archive_version_number());
35-
sprintf(buff, "libarchive %d.%d.%d",
35+
snprintf(buff, sizeof(buff), "libarchive %d.%d.%d",
3636
archive_version_number() / 1000000,
3737
(archive_version_number() / 1000) % 1000,
3838
archive_version_number() % 1000);

libarchive/test/test_read_truncated_filter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test_truncation(const char *compression,
8181
archive_entry_set_filetype(ae, AE_IFREG);
8282
archive_entry_set_size(ae, datasize);
8383
for (i = 0; i < 100; i++) {
84-
sprintf(path, "%s%d", compression, i);
84+
snprintf(path, sizeof(path), "%s%d", compression, i);
8585
archive_entry_copy_pathname(ae, path);
8686
failure("%s", path);
8787
if (!assertEqualIntA(a, ARCHIVE_OK,
@@ -123,7 +123,7 @@ test_truncation(const char *compression,
123123
assert(NULL != archive_error_string(a));
124124
break;
125125
}
126-
sprintf(path, "%s%d", compression, i);
126+
snprintf(path, sizeof(path), "%s%d", compression, i);
127127
assertEqualString(path, archive_entry_pathname(ae));
128128
if (datasize != (size_t)archive_read_data(a, data, datasize)) {
129129
failure("Should have non-NULL error message for %s",

libarchive/test/test_tar_large.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ DEFINE_TEST(test_tar_large)
224224
*/
225225
for (i = 0; tests[i] != 0; i++) {
226226
assert((ae = archive_entry_new()) != NULL);
227-
sprintf(namebuff, "file_%d", i);
227+
snprintf(namebuff, sizeof(namebuff), "file_%d", i);
228228
archive_entry_copy_pathname(ae, namebuff);
229229
archive_entry_set_mode(ae, S_IFREG | 0755);
230230
filesize = tests[i];
@@ -271,7 +271,7 @@ DEFINE_TEST(test_tar_large)
271271
*/
272272
for (i = 0; tests[i] > 0; i++) {
273273
assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
274-
sprintf(namebuff, "file_%d", i);
274+
snprintf(namebuff, sizeof(namebuff), "file_%d", i);
275275
assertEqualString(namebuff, archive_entry_pathname(ae));
276276
assert(tests[i] == archive_entry_size(ae));
277277
}

libarchive/test/test_write_disk_secure744.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ DEFINE_TEST(test_write_disk_secure744)
7575
archive_entry_free(ae);
7676

7777
*p++ = '/';
78-
sprintf(p, "target%d", n);
78+
snprintf(p, buff_size - (p - buff), "target%d", n);
7979

8080
/* Try to create a file through the symlink, should fail. */
8181
assert((ae = archive_entry_new()) != NULL);

libarchive/test/test_write_filter_b64encode.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ DEFINE_TEST(test_write_filter_b64encode)
6464
assert((ae = archive_entry_new()) != NULL);
6565
archive_entry_set_filetype(ae, AE_IFREG);
6666
archive_entry_set_size(ae, datasize);
67-
sprintf(path, "file%03d", i);
67+
snprintf(path, sizeof(path), "file%03d", i);
6868
archive_entry_copy_pathname(ae, path);
6969
assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
7070
assertA(datasize
@@ -79,7 +79,7 @@ DEFINE_TEST(test_write_filter_b64encode)
7979
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
8080
assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used1));
8181
for (i = 0; i < 99; i++) {
82-
sprintf(path, "file%03d", i);
82+
snprintf(path, sizeof(path), "file%03d", i);
8383
if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae)))
8484
break;
8585
assertEqualString(path, archive_entry_pathname(ae));
@@ -111,7 +111,7 @@ DEFINE_TEST(test_write_filter_b64encode)
111111
assertEqualIntA(a, ARCHIVE_OK,
112112
archive_write_open_memory(a, buff, buffsize, &used2));
113113
for (i = 0; i < 99; i++) {
114-
sprintf(path, "file%03d", i);
114+
snprintf(path, sizeof(path), "file%03d", i);
115115
assert((ae = archive_entry_new()) != NULL);
116116
archive_entry_copy_pathname(ae, path);
117117
archive_entry_set_size(ae, datasize);
@@ -128,7 +128,7 @@ DEFINE_TEST(test_write_filter_b64encode)
128128
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
129129
assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used2));
130130
for (i = 0; i < 99; i++) {
131-
sprintf(path, "file%03d", i);
131+
snprintf(path, sizeof(path), "file%03d", i);
132132
if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
133133
break;
134134
assertEqualString(path, archive_entry_pathname(ae));

0 commit comments

Comments
 (0)