Skip to content

Commit

Permalink
http-backend: Fix bad treatment of uintmax_t in Content-Length
Browse files Browse the repository at this point in the history
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 <tarmigan+git@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Shawn O. Pearce authored and Junio C Hamano committed Nov 13, 2009
1 parent 2036663 commit 4a5328d
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions http-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void hdr_str(const char *name, const char *value)
format_write(1, "%s: %s\r\n", name, value);
}

static void hdr_int(const char *name, size_t value)
static void hdr_int(const char *name, uintmax_t value)
{
format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
}
Expand Down Expand Up @@ -216,22 +216,19 @@ static void send_local_file(const char *the_type, const char *name)
char *buf = xmalloc(buf_alloc);
int fd;
struct stat sb;
size_t size;

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

size = xsize_t(sb.st_size);

hdr_int(content_length, size);
hdr_int(content_length, sb.st_size);
hdr_str(content_type, the_type);
hdr_date(last_modified, sb.st_mtime);
end_headers();

while (size) {
for (;;) {
ssize_t n = xread(fd, buf, buf_alloc);
if (n < 0)
die_errno("Cannot read '%s'", p);
Expand Down

0 comments on commit 4a5328d

Please sign in to comment.