Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin.c: fix signed integer overflow in jv2tm #3070

Merged
merged 1 commit into from
Mar 19, 2024

Conversation

emanuele6
Copy link
Member

@emanuele6 emanuele6 commented Mar 15, 2024

jv2tm now properly clamps large number values to a signed 32-bit integer and rejects nan.

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=65885

@emanuele6 emanuele6 force-pushed the fixjv2tm branch 3 times, most recently from 72ceb41 to 60e95a6 Compare March 15, 2024 14:31
@emanuele6
Copy link
Member Author

emanuele6 commented Mar 15, 2024

This does not solve the problem fully; it still underflows if you use -1e31 instead of 1e31 because of the tm->tm_year -= 1900; in jv2tm

src/builtin.c Outdated
offsetof(struct tm, tm_yday),
};

jv_array_foreach(a, i, n) {
Copy link
Member Author

@emanuele6 emanuele6 Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the TO_TM_FIELD macro repeated 8 times with a loop over offsets

src/builtin.c Outdated Show resolved Hide resolved
@emanuele6 emanuele6 force-pushed the fixjv2tm branch 3 times, most recently from bfd895c to 5f61b1f Compare March 15, 2024 16:07
tests/jq.test Outdated Show resolved Hide resolved
Now, time functions accept array inputs even if they don't have all the
elements, 0 will be assumed if a value is not present.

Also, jv2tm now properly clamps large number values to a signed 32-bit
integer and rejects nan.

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=65885
src/builtin.c Show resolved Hide resolved
@emanuele6 emanuele6 merged commit bc96146 into jqlang:master Mar 19, 2024
28 checks passed
@emanuele6 emanuele6 deleted the fixjv2tm branch March 19, 2024 01:02
@itchyny
Copy link
Contributor

itchyny commented Aug 20, 2024

Why did you leave mktime not accepting the input strftime can recognize?

 $ ./jq -n '[2024,7,20] | todate, mktime'           
"2024-08-20T00:00:00Z"
jq: error (at <unknown>): mktime requires parsed datetime inputs

@emanuele6
Copy link
Member Author

I didn't touch todate/strftime directly; it looks like mktime doesn't accept those values because there was an unnecessary extra check that requires the length of the input array to be at least 6:

  if (jv_array_length(jv_copy(a)) < 6)
    return ret_error(a, jv_string("mktime requires parsed datetime inputs"));

That can be removed now

emanuele6 added a commit to emanuele6/jq-1 that referenced this pull request Aug 20, 2024
This check was redundant and useless since jv2tm() used to require 8
elements, and errored anyway if not enough elements were provided.
After commit bc96146, this check also prevents mktime/0 from accepting
now supported0 inputs with less than 8 elements.

Ref: jqlang#3070 (comment)

Reported-by: itchyny <itchyny@cybozu.co.jp>
emanuele6 added a commit to emanuele6/jq-1 that referenced this pull request Aug 20, 2024
This check was redundant and useless since jv2tm() used to require 8
elements, and errored anyway if not enough elements were provided.
After commit bc96146, this check also prevents mktime/0 from accepting
now supported inputs with less than 8 elements.

Ref: jqlang#3070 (comment)

Reported-by: itchyny <itchyny@cybozu.co.jp>
itchyny pushed a commit that referenced this pull request Aug 21, 2024
This check was redundant and useless since jv2tm() used to require 8
elements, and errored anyway if not enough elements were provided.
After commit bc96146, this check also prevents mktime/0 from accepting
now supported inputs with less than 8 elements.

Ref: #3070 (comment)

Reported-by: itchyny <itchyny@cybozu.co.jp>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants