aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archival/libarchive/get_header_ar.c32
1 files changed, 20 insertions, 12 deletions
diff --git a/archival/libarchive/get_header_ar.c b/archival/libarchive/get_header_ar.c
index 23c412496..f655585fe 100644
--- a/archival/libarchive/get_header_ar.c
+++ b/archival/libarchive/get_header_ar.c
@@ -8,11 +8,19 @@
8#include "bb_archive.h" 8#include "bb_archive.h"
9#include "ar.h" 9#include "ar.h"
10 10
11static unsigned read_num(const char *str, int base) 11/* WARNING: Clobbers str[len], so fields must be read in reverse order! */
12static unsigned read_num(char *str, int base, int len)
12{ 13{
14 int err;
15
16 /* ar fields are fixed length text strings (padded with spaces).
17 * Ensure bb_strtou doesn't read past the field in case the full
18 * width is used. */
19 str[len] = 0;
20
13 /* This code works because 21 /* This code works because
14 * on misformatted numbers bb_strtou returns all-ones */ 22 * on misformatted numbers bb_strtou returns all-ones */
15 int err = bb_strtou(str, NULL, base); 23 err = bb_strtou(str, NULL, base);
16 if (err == -1) 24 if (err == -1)
17 bb_error_msg_and_die("invalid ar header"); 25 bb_error_msg_and_die("invalid ar header");
18 return err; 26 return err;
@@ -51,11 +59,8 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
51 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n') 59 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
52 bb_error_msg_and_die("invalid ar header"); 60 bb_error_msg_and_die("invalid ar header");
53 61
54 /* FIXME: more thorough routine would be in order here 62 typed->size = size = read_num(ar.formatted.size, 10,
55 * (we have something like that in tar) 63 sizeof(ar.formatted.size));
56 * but for now we are lax. */
57 ar.formatted.magic[0] = '\0'; /* else 4G-2 file will have size="4294967294`\n..." */
58 typed->size = size = read_num(ar.formatted.size, 10);
59 64
60 /* special filenames have '/' as the first character */ 65 /* special filenames have '/' as the first character */
61 if (ar.formatted.name[0] == '/') { 66 if (ar.formatted.name[0] == '/') {
@@ -86,11 +91,13 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
86 /* Only size is always present, the rest may be missing in 91 /* Only size is always present, the rest may be missing in
87 * long filename pseudo file. Thus we decode the rest 92 * long filename pseudo file. Thus we decode the rest
88 * after dealing with long filename pseudo file. 93 * after dealing with long filename pseudo file.
94 * Note that the fields MUST be read in reverse order as
95 * read_num() clobbers the next byte after the field!
89 */ 96 */
90 typed->mode = read_num(ar.formatted.mode, 8); 97 typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode));
91 typed->mtime = read_num(ar.formatted.date, 10); 98 typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid));
92 typed->uid = read_num(ar.formatted.uid, 10); 99 typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid));
93 typed->gid = read_num(ar.formatted.gid, 10); 100 typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date));
94 101
95#if ENABLE_FEATURE_AR_LONG_FILENAMES 102#if ENABLE_FEATURE_AR_LONG_FILENAMES
96 if (ar.formatted.name[0] == '/') { 103 if (ar.formatted.name[0] == '/') {
@@ -98,7 +105,8 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
98 105
99 /* The number after the '/' indicates the offset in the ar data section 106 /* The number after the '/' indicates the offset in the ar data section
100 * (saved in ar_long_names) that conatains the real filename */ 107 * (saved in ar_long_names) that conatains the real filename */
101 long_offset = read_num(&ar.formatted.name[1], 10); 108 long_offset = read_num(&ar.formatted.name[1], 10,
109 sizeof(ar.formatted.name) - 1);
102 if (long_offset >= ar_long_name_size) { 110 if (long_offset >= ar_long_name_size) {
103 bb_error_msg_and_die("can't resolve long filename"); 111 bb_error_msg_and_die("can't resolve long filename");
104 } 112 }