Huny's Dev Blog
huny.dev

Why C strtoull Accepts -1: Strict Decimal uint64_t Parsing with Range Checks

Reproduce unsigned conversion of negative input and build a strict C parser with digit, errno, end-pointer, and UINT64_MAX checks, tested against 12 strings and NULL arguments.

Hun Jang
Hun Jang Nov 23, 2026

In C, an unsigned return type does not mean strtoull() rejects a leading minus sign when reading a setting that must not be negative. To accept only decimal uint64_t values, validate the entire string's syntax first, then check the conversion range and end position. This article reproduces a successful conversion of -1 on Linux and implements a parser that distinguishes signs, whitespace, trailing characters, and overflow.

AI-generated conceptual illustration of a numeric input validation gate and rejected irregular fragments. This is not an exact parser flowchart.

Written and documentation checked on 2026-11-23. The code was compiled in C11 mode and run on Linux x86_64 with GCC 14.2.0 and glibc 2.41. Results from other operating systems or C libraries are not included.

An unsigned return type does not reject negative input

strtoull() accepts an optional sign. A leading minus sign can cause unsigned negation of the converted result. The NetBSD conversion documentation explicitly notes that the return value and errno alone cannot tell you whether the input had a minus sign.

Consequently, checking errno == 0 and complete string consumption can still admit -1. The result in the tested environment was 18446744073709551615. This is that environment's ULLONG_MAX; the explanation does not assume unsigned long long is exactly 64 bits on every C implementation.

Define the accepted syntax and range first

This parser uses the following contract. It does not implement the complete JSON number grammar or a permissive human-input format.

Input condition
Handling
One or more digits from 0 through 9
Convert as decimal
-, +, whitespace, a decimal point, or trailing characters
Syntax error
010
Decimal 10 because leading zeros are allowed
18446744073709551615
Accept as the uint64 maximum
18446744073709551616 or larger
Range error
Empty string or a NULL argument
Return a syntax error

Character range comparisons restrict the input to ASCII digits in this Linux environment. The conversion base is fixed at 10 rather than 0. Since the library itself can accept leading whitespace, signs, and base-specific notation, define the application's grammar separately. Linux strtoul and strtoull documentation

Convert through uintmax_t, then check the uint64_t range

Save the following as parse_u64.c. parse_u64_decimal() is the reusable function; main() contains the reproduction and boundary tests. The function updates *out only on success.

The following commands were used to compile with warnings treated as errors and run the program. Do not use -DNDEBUG, because the checks rely on assertions.

The actual output was:

strtoumax() converts to uintmax_t. Check errno == ERANGE for overflow of that type, then compare against UINT64_MAX in case the destination type is narrower. Treating the maximum return value itself as an error would reject a valid maximum-value input. strtoumax return values and errors

The order is syntax, conversion, destination range, and result assignment. Clear errno immediately before conversion so an earlier error is not reused. Reject an end pointer equal to the input start or one that does not point to the terminator. This implementation retains the end-position check even after its digits-only precheck. Conversion end position and errno

Assertions verify each result status and successful value, as well as preservation of the initial output value 123 after a failure. consumed=1 means the entire string was consumed, not that one character was read.

Boundaries when connecting it to server input

The string argument must point to a readable, NUL-terminated string. Checking for NULL does not validate an arbitrary pointer. Do not pass a length-delimited network buffer directly: first validate its original length, maximum allowed length, and policy for embedded NUL bytes. For example, if 1\0junk is passed as a C string, this function cannot see junk after the first NUL.

A value within the uint64_t range is not automatically a valid port number or allocation size. After conversion, separately check the field's minimum, maximum, and policy for zero, and verify the destination range before casting to a smaller integer type.

This article verified 12 numeric strings and two NULL-argument checks locally. Processing cost for long input, fuzzing, integration into a real server, non-ASCII execution character sets, and other libc implementations were not tested. If an interface must allow surrounding whitespace or +, change the test contract first.

한국어 버전: C strtoull이 -1을 허용하는 이유: uint64_t 십진 입력을 엄격하게 검증하기C strtoull이 -1을 허용하는 이유: uint64_t 십진 입력을 엄격하게 검증하기

추천 글