nodejs / http-parser

http request/response parser for c
MIT License
6.35k stars 1.54k forks source link

parser->nread is incorrect at on_headers_complete #451

Open jcwcmimik opened 5 years ago

jcwcmimik commented 5 years ago

master branch as of commit 4dae120, http_parser.c:1788

Neither SET_ERRNO nor RETURN are necessarily called in this case (s_headers_almost_done) and thus when on_headers_complete(parser) is called, parser still contains an old nread value (0 if it's the first parse attempt).

In my test cases, inserting parser->nread = nread; just before line 1788 solves the issue, but based on that assignment being contained solely within the SET_ERRNO and RETURN defines, someone more familiary with the code may wish to implement it elsewhere.

bnoordhuis commented 5 years ago

p->nread is only for tracking if we moved past HTTP_MAX_HEADER_SIZE. At line 1788, that doesn't matter anymore because we're done parsing headers.

I've fixed off-by-one errors in the past but that's not that useful here, it'd only benefit downstream users that inappropriately use p->nread. It's private and marked as such in http_parser.h but some projects still use it.