mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-10 11:55:49 +00:00
Bug 1254061 - Rewrite nsHttp::ParseInt64 r=mcmanus
MozReview-Commit-ID: FXjTBah4OSd * * * [mq]: test MozReview-Commit-ID: 5YT1jWVb21K --HG-- extra : rebase_source : 8bd1b91fd83f1f1f2d815d12baa5002f69e933f2
This commit is contained in:
parent
8b918e193d
commit
518b567d5a
@ -294,19 +294,25 @@ nsHttp::FindToken(const char *input, const char *token, const char *seps)
|
||||
bool
|
||||
nsHttp::ParseInt64(const char *input, const char **next, int64_t *r)
|
||||
{
|
||||
const char *start = input;
|
||||
*r = 0;
|
||||
while (*input >= '0' && *input <= '9') {
|
||||
int64_t next = 10 * (*r) + (*input - '0');
|
||||
if (next < *r) // overflow?
|
||||
return false;
|
||||
*r = next;
|
||||
++input;
|
||||
}
|
||||
if (input == start) // nothing parsed?
|
||||
MOZ_ASSERT(input);
|
||||
MOZ_ASSERT(r);
|
||||
|
||||
char *end = nullptr;
|
||||
errno = 0; // Clear errno to make sure its value is set by strtoll
|
||||
int64_t value = strtoll(input, &end, /* base */ 10);
|
||||
|
||||
// Fail if: - the parsed number overflows.
|
||||
// - the end points to the start of the input string.
|
||||
// - we parsed a negative value. Consumers don't expect that.
|
||||
if (errno != 0 || end == input || value < 0) {
|
||||
LOG(("nsHttp::ParseInt64 value=%ld errno=%d", value, errno));
|
||||
return false;
|
||||
if (next)
|
||||
*next = input;
|
||||
}
|
||||
|
||||
if (next) {
|
||||
*next = end;
|
||||
}
|
||||
*r = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user