[BUG] libc/time: strptime() numeric_range upper bound is off by one and accepts invalid dates/times

Open Beginner friendly
#22,758 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c

Research direction

Start in libs/libc/time/lib_strptime.c and inspect numeric_range plus the format specifiers that set min and range. Check how the digit loop and upper-bound validation handle the listed values, then run or add focused strptime() checks for each affected specifier. Done means out-of-range values return NULL while the stated valid ranges still parse.

Written by the indexing model from the issue text.

Description

Arch: all Area: Posix OS: Linux
Description / Steps to reproduce the issue
Summary

In libs/libc/time/lib_strptime.c, numeric_range checks *dest > min + range instead of *dest >= min + range:

            *dest = 0;
            for (i = 1; i <= min + range && isdigit(*s); i *= 10)
              {
                *dest = *dest * 10 + *s++ - '0';
              }

            if (*dest < min || *dest > min + range)
              {
                return NULL;
              }
Root Cause

Every specifier jumping to numeric_range sets range to the count of valid values (so the inclusive upper bound is min + range - 1):

  • %H: min=0, range=24 (valid 0..23, accepts 24)
  • %M: min=0, range=60 (valid 0..59, accepts 60)
  • %S: min=0, range=61 (valid 0..60, accepts 61)
  • %m: min=1, range=12 (valid 1..12, accepts 13 -> tm_mon = 12)
  • %d/%e: min=1, range=31 (valid 1..31, accepts 32)
  • %I: min=1, range=12 (valid 1..12, accepts 13)
  • %j: min=1, range=366 (valid 1..366, accepts 367)
  • %w: min=0, range=7 (valid 0..6, accepts 7)
  • %u: min=1, range=7 (valid 1..7, accepts 8)
  • %U/%W: min=0, range=54 (valid 0..53, accepts 54)
  • %V: min=1, range=53 (valid 1..53, accepts 54)
Steps to Reproduce

Compile libs/libc/time/lib_strptime.c and parse out-of-range values (Ubuntu 24.04, x86_64, gcc 13.3.0):

strptime("24:00:00", "%H:%M:%S"): glibc=NULL nuttx=MATCH
strptime("12:60:00", "%H:%M:%S"): glibc=NULL nuttx=MATCH
strptime("2026-13-01", "%Y-%m-%d"): glibc=NULL nuttx=MATCH
strptime("2026-01-32", "%Y-%m-%d"): glibc=NULL nuttx=MATCH
strptime("13", "%I"): glibc=NULL nuttx=MATCH
strptime("367", "%j"): glibc=NULL nuttx=MATCH
strptime("7", "%w"): glibc=NULL nuttx=MATCH
strptime("8", "%u"): glibc=NULL nuttx=MATCH
strptime("54", "%U"): glibc=NULL nuttx=MATCH
strptime("54", "%V"): glibc=NULL nuttx=MATCH
Expected Behavior

Use i <= min + range - 1 for the digit loop and reject *dest < min || *dest >= min + range with NULL.

On which OS does this issue occur?

[OS: Linux]

What is the version of your OS?

Ubuntu 24.04

NuttX Version

master (cf03942d)

Issue Architecture

[Arch: all]

Issue Area

[Area: Posix]

Host information

Architecture-independent C bug in libs/libc/time/lib_strptime.c. Reproduced on Ubuntu 24.04 x86_64 with gcc 13.3.0.

Verification
  • I have verified before submitting the report.
Dominant language
C
Stars
4.1k
Forks
1.8k
Avg merge
1d 2h
Merged PRs (30d)
881

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/nuttx

All issues in apache/nuttx

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.