Skip to content

Commit

Permalink
Fixed #124: Can't parse INT_MIN
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Jul 29, 2022
1 parent 5d51269 commit 44cfe97
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 138 deletions.
38 changes: 32 additions & 6 deletions parse_date.re
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ static timelib_sll timelib_get_nr_ex(const char **ptr, int max_length, int *scan
}
++*ptr;
}

begin = *ptr;
while ((**ptr >= '0') && (**ptr <= '9') && len < max_length) {
++*ptr;
Expand Down Expand Up @@ -541,24 +542,49 @@ static timelib_sll timelib_get_frac_nr(const char **ptr)

static timelib_ull timelib_get_signed_nr(Scanner *s, const char **ptr, int max_length)
{
timelib_ull dir = 1;
char *str, *str_ptr;
timelib_sll tmp_nr = 0;
int len = 0;

str = timelib_calloc(1, max_length + 2); // for sign and \0
str_ptr = str;

while (((**ptr < '0') || (**ptr > '9')) && (**ptr != '+') && (**ptr != '-')) {
if (**ptr == '\0') {
add_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Found unexpected data");
timelib_free(str);
return 0;
}
++*ptr;
}

while (**ptr == '+' || **ptr == '-')
{
if (**ptr == '-') {
dir *= -1;
if ((**ptr == '+') || (**ptr == '-')) {
*str_ptr = **ptr;
++*ptr;
++str_ptr;
}

while (((**ptr < '0') || (**ptr > '9'))) {
if (**ptr == '\0') {
timelib_free(str);
add_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Found unexpected data");
return 0;
}
++*ptr;
}
return dir * timelib_get_nr(ptr, max_length);

while ((**ptr >= '0') && (**ptr <= '9') && len < max_length) {
*str_ptr = **ptr;
++*ptr;
++str_ptr;
++len;
}

tmp_nr = strtoll(str, NULL, 10);

timelib_free(str);

return tmp_nr;
}

static timelib_sll timelib_lookup_relative_text(const char **ptr, int *behavior)
Expand Down
137 changes: 5 additions & 132 deletions tests/c/parse_date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3639,138 +3639,6 @@ TEST(parse_date, relative_02)
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_03)
{
test_parse("++2 sec");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(2, t->relative.s);
}

TEST(parse_date, relative_04)
{
test_parse("+-2 secs");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_05)
{
test_parse("-+2 sec");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_06)
{
test_parse("--2 secs");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(2, t->relative.s);
}

TEST(parse_date, relative_07)
{
test_parse("+++2 sec");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(2, t->relative.s);
}

TEST(parse_date, relative_08)
{
test_parse("++-2 secs");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_09)
{
test_parse("+-+2 sec");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_10)
{
test_parse("+--2 secs");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(2, t->relative.s);
}

TEST(parse_date, relative_11)
{
test_parse("-++2 sec");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_12)
{
test_parse("-+-2 secs");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(2, t->relative.s);
}

TEST(parse_date, relative_13)
{
test_parse("--+2 sec");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(2, t->relative.s);
}

TEST(parse_date, relative_14)
{
test_parse("---2 secs");
LONGS_EQUAL(0, t->relative.y);
LONGS_EQUAL(0, t->relative.m);
LONGS_EQUAL(0, t->relative.d);
LONGS_EQUAL(0, t->relative.h);
LONGS_EQUAL(0, t->relative.i);
LONGS_EQUAL(-2, t->relative.s);
}

TEST(parse_date, relative_15)
{
test_parse("+2 sec ago");
Expand Down Expand Up @@ -5942,3 +5810,8 @@ TEST(parse_date, timetiny24_06)
POINTERS_EQUAL(NULL, t->tz_abbr);
}

TEST(parse_date, gh_124a)
{
test_parse("@-92233720368547758088");
LONGS_EQUAL(0x8000000000000000, t->relative.s);
}

0 comments on commit 44cfe97

Please sign in to comment.