Skip to content

Commit 4a5328d

Browse files
spearcegitster
authored andcommitted
http-backend: Fix bad treatment of uintmax_t in Content-Length
Our Content-Length needs to report an off_t, which could be larger precision than size_t on this system (e.g. 32 bit binary built with 64 bit large file support). We also shouldn't be passing a size_t parameter to printf when we've used PRIuMAX as the format specifier. Fix both issues by using uintmax_t for the hdr_int() routine, allowing strbuf's size_t to automatically upcast, and off_t to always fit. Also fixed the copy loop we use inside of send_local_file(), we never actually updated the size variable so we might as well not use it. Reported-by: Tarmigan <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2036663 commit 4a5328d

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

http-backend.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static void hdr_str(const char *name, const char *value)
134134
format_write(1, "%s: %s\r\n", name, value);
135135
}
136136

137-
static void hdr_int(const char *name, size_t value)
137+
static void hdr_int(const char *name, uintmax_t value)
138138
{
139139
format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
140140
}
@@ -216,22 +216,19 @@ static void send_local_file(const char *the_type, const char *name)
216216
char *buf = xmalloc(buf_alloc);
217217
int fd;
218218
struct stat sb;
219-
size_t size;
220219

221220
fd = open(p, O_RDONLY);
222221
if (fd < 0)
223222
not_found("Cannot open '%s': %s", p, strerror(errno));
224223
if (fstat(fd, &sb) < 0)
225224
die_errno("Cannot stat '%s'", p);
226225

227-
size = xsize_t(sb.st_size);
228-
229-
hdr_int(content_length, size);
226+
hdr_int(content_length, sb.st_size);
230227
hdr_str(content_type, the_type);
231228
hdr_date(last_modified, sb.st_mtime);
232229
end_headers();
233230

234-
while (size) {
231+
for (;;) {
235232
ssize_t n = xread(fd, buf, buf_alloc);
236233
if (n < 0)
237234
die_errno("Cannot read '%s'", p);

0 commit comments

Comments
 (0)