Skip to content

Commit f2f0267

Browse files
peffgitster
authored andcommitted
archive-tar: use xsnprintf for trivial formatting
When we generate tar headers, we sprintf() values directly into a struct with the fixed-size header values. For the most part this is fine, as we are formatting small values (e.g., the octal format of "mode & 0x7777" is of fixed length). But it's still a good idea to use xsnprintf here. It communicates to readers what our expectation is, and it provides a run-time check that we are not overflowing the buffers. The one exception here is the mtime, which comes from the epoch time of the commit we are archiving. For sane values, this fits into the 12-byte value allocated in the header. But since git can handle 64-bit times, if I claim to be a visitor from the year 10,000 AD, I can overflow the buffer. This turns out to be harmless, as we simply overflow into the chksum field, which is then overwritten. This case is also best as an xsnprintf. It should never come up, short of extremely malformed dates, and in that case we are probably better off dying than silently truncating the date value (and we cannot expand the size of the buffer, since it is dictated by the ustar format). Our friends in the year 5138 (when we legitimately flip to a 12-digit epoch) can deal with that problem then. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5096d49 commit f2f0267

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

archive-tar.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,21 @@ static void prepare_header(struct archiver_args *args,
167167
struct ustar_header *header,
168168
unsigned int mode, unsigned long size)
169169
{
170-
sprintf(header->mode, "%07o", mode & 07777);
171-
sprintf(header->size, "%011lo", S_ISREG(mode) ? size : 0);
172-
sprintf(header->mtime, "%011lo", (unsigned long) args->time);
170+
xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
171+
xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
172+
xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
173173

174-
sprintf(header->uid, "%07o", 0);
175-
sprintf(header->gid, "%07o", 0);
174+
xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
175+
xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
176176
strlcpy(header->uname, "root", sizeof(header->uname));
177177
strlcpy(header->gname, "root", sizeof(header->gname));
178-
sprintf(header->devmajor, "%07o", 0);
179-
sprintf(header->devminor, "%07o", 0);
178+
xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
179+
xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
180180

181181
memcpy(header->magic, "ustar", 6);
182182
memcpy(header->version, "00", 2);
183183

184-
sprintf(header->chksum, "%07o", ustar_header_chksum(header));
184+
snprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
185185
}
186186

187187
static int write_extended_header(struct archiver_args *args,
@@ -193,7 +193,7 @@ static int write_extended_header(struct archiver_args *args,
193193
memset(&header, 0, sizeof(header));
194194
*header.typeflag = TYPEFLAG_EXT_HEADER;
195195
mode = 0100666;
196-
sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
196+
xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
197197
prepare_header(args, &header, mode, size);
198198
write_blocked(&header, sizeof(header));
199199
write_blocked(buffer, size);
@@ -235,8 +235,8 @@ static int write_tar_entry(struct archiver_args *args,
235235
memcpy(header.prefix, path, plen);
236236
memcpy(header.name, path + plen + 1, rest);
237237
} else {
238-
sprintf(header.name, "%s.data",
239-
sha1_to_hex(sha1));
238+
xsnprintf(header.name, sizeof(header.name), "%s.data",
239+
sha1_to_hex(sha1));
240240
strbuf_append_ext_header(&ext_header, "path",
241241
path, pathlen);
242242
}
@@ -259,8 +259,8 @@ static int write_tar_entry(struct archiver_args *args,
259259

260260
if (S_ISLNK(mode)) {
261261
if (size > sizeof(header.linkname)) {
262-
sprintf(header.linkname, "see %s.paxheader",
263-
sha1_to_hex(sha1));
262+
xsnprintf(header.linkname, sizeof(header.linkname),
263+
"see %s.paxheader", sha1_to_hex(sha1));
264264
strbuf_append_ext_header(&ext_header, "linkpath",
265265
buffer, size);
266266
} else

0 commit comments

Comments
 (0)