Skip to content

Commit

Permalink
Merge branch 'bug/30'
Browse files Browse the repository at this point in the history
* bug/30:
  minor, rewrite __strpd_std() in terms of strtoi() to work around bug/30
  hygiene, hand-craft strtoi() so the limiting logic of strtoi_lim() doesn't apply
  test, provide regression case for bug/30 problem
  • Loading branch information
hroptatyr committed Feb 26, 2015
2 parents 45998a9 + 2dd1aca commit b194e3d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
16 changes: 9 additions & 7 deletions lib/date-core-strpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ __strpd_std(const char *str, char **ep)

d = strpd_initialiser();
/* read the year */
if ((d.y = strtoi_lim(sp, &sp, DT_MIN_YEAR, DT_MAX_YEAR)) < 0 ||
*sp++ != '-') {
d.y = strtoi(sp, &sp);
if (d.y < DT_MIN_YEAR || d.y > DT_MAX_YEAR || *sp++ != '-') {
goto fucked;
}
/* check for ywd dates */
if (UNLIKELY(*sp == 'W')) {
/* brilliant */
if ((sp++, d.c = strtoi_lim(sp, &sp, 0, 53)) < 0 ||
*sp++ != '-') {
sp++, d.c = strtoi(sp, &sp);
if (d.c < 0 || d.c > 53 || *sp++ != '-') {
goto fucked;
}
d.flags.c_wcnt_p = 1;
Expand All @@ -202,7 +202,8 @@ __strpd_std(const char *str, char **ep)
}
/* read the month, then day count */
with (const char *tmp) {
if ((d.m = strtoi_lim(sp, &tmp, 0, 366)) < 0) {
d.m = strtoi(sp, &tmp);
if (d.m < 0 || d.m > 366) {
goto fucked;
} else if (UNLIKELY(*tmp != '-')) {
/* oh, could be an ordinal date */
Expand All @@ -216,7 +217,7 @@ __strpd_std(const char *str, char **ep)
goto fucked;
}
sp = tmp;
} else if ((d.d = strtoi_lim(++tmp, &sp, 0, 31)) < 0) {
} else if (d.d = strtoi(++tmp, &sp), d.d < 0 || d.d > 31) {
/* didn't work, fuck off */
goto fucked;
}
Expand All @@ -232,7 +233,8 @@ __strpd_std(const char *str, char **ep)
d.d = 0;
sp++;
dow:
if ((d.w = strtoi_lim(sp, &sp, 0, GREG_DAYS_P_WEEK)) < 0) {
if (d.w = strtoi(sp, &sp),
d.w < 0 || (unsigned int)d.w > GREG_DAYS_P_WEEK) {
/* didn't work, fuck off */
goto fucked;
}
Expand Down
10 changes: 7 additions & 3 deletions lib/strops.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,21 @@ strtoi(const char *str, const char **ep)
{
const char *sp = str;
bool negp = false;
int32_t res;
int32_t res = 0;

if (*str == '-') {
negp = true;
sp++;
}
if ((res = strtoi_lim(sp, ep, 0, INT32_MAX)) < 0) {
*ep = str;
while (res < INT32_MAX / 10 && (unsigned char)(*sp ^ '0') < 10U) {
res *= 10, res += (unsigned char)(*sp++ ^ '0');
}
if (UNLIKELY(sp == str)) {
res = -1;
} else if (negp) {
res = -res;
}
*ep = (char*)sp;
return res;
}

Expand Down
2 changes: 2 additions & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ dt_tests += dconv.115.clit
dt_tests += dconv.116.clit
dt_tests += dconv.117.clit
dt_tests += dconv.118.clit
dt_tests += dconv.119.clit
dt_tests += dconv.120.clit

dt_tests += dadd.001.clit
dt_tests += dadd.002.clit
Expand Down
6 changes: 6 additions & 0 deletions test/dconv.119.clit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/clitoris ## -*- shell-script -*-

$ ! echo "2015-2015-00-12-12" | dconv
$

## dconv.119.clit ends here
6 changes: 6 additions & 0 deletions test/dconv.120.clit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/clitoris ## -*- shell-script -*-

$ ! dconv "2015-2015-00-12-12"
$

## dconv.120.clit ends here

0 comments on commit b194e3d

Please sign in to comment.