Skip to content

Commit

Permalink
Fix crash when a file has a large (year 10,000+) mtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
emikulic committed Oct 2, 2022
1 parent 1f16629 commit 1eb6daa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 6 additions & 4 deletions darkhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,9 @@ static void logencode(const char *src, char *dest) {
static char *clf_date(char *dest, const time_t when) {
time_t when_copy = when;
if (strftime(dest, CLF_DATE_LEN,
"[%d/%b/%Y:%H:%M:%S %z]", localtime(&when_copy)) == 0)
errx(1, "strftime() failed [%s]", dest);
"[%d/%b/%Y:%H:%M:%S %z]", localtime(&when_copy)) == 0) {
dest[0] = 0;
}
return dest;
}

Expand Down Expand Up @@ -1456,8 +1457,9 @@ static void poll_check_timeout(struct connection *conn) {
static char *rfc1123_date(char *dest, const time_t when) {
time_t when_copy = when;
if (strftime(dest, DATE_LEN,
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&when_copy)) == 0)
errx(1, "strftime() failed [%s]", dest);
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&when_copy)) == 0) {
dest[0] = 0;
}
return dest;
}

Expand Down
19 changes: 19 additions & 0 deletions devel/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import os
import random
import time

WWWROOT = "tmp.httpd.tests"

Expand Down Expand Up @@ -470,6 +471,24 @@ def test_largefile_3(self): self.drive_start(3)
class TestLargeFile4G(TestLargeFile2G):
BOUNDARY = 1<<32

class TestLargeMtime(TestHelper):
def setUp(self):
self.url = '/large_mtime'
self.fn = WWWROOT + self.url
with open(self.fn, 'wb') as f:
f.write(b'x')
# A timestamp in the year 10,000
t = int(time.mktime((10000,3,14,1,2,3,0,0,-1)))
os.utime(self.fn, (t, t))

def tearDown(self):
os.unlink(self.fn)

def test_file_get(self):
resp = self.get(self.url)
status, hdrs, body = parse(resp)
self.assertContains(status, "200 OK")

if __name__ == '__main__':
setUpModule()
unittest.main()
Expand Down

0 comments on commit 1eb6daa

Please sign in to comment.