Skip to content

Fix integer overflow when parsing CGI array indices - #1784

Closed
JWWTSL wants to merge 1 commit into
OpenPrinting:masterfrom
JWWTSL:fix-cgi-atoi
Closed

JWWTSL wants to merge 1 commit into
OpenPrinting:masterfrom
JWWTSL:fix-cgi-atoi

Conversation

@JWWTSL

@JWWTSL JWWTSL commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Summary

atoi() is used to parse the numeric suffix of CGI form field names into an
array index in cgi-bin/var.c. On values that exceed INT_MAX this is
undefined behavior, and the result cannot be reliably validated by the
subsequent range check in cgiSetArray().

Add a _cups_parse_index() helper that uses strtol(), resets and checks
errno for ERANGE, and validates the parsed value before converting it to
an index. Use it at the three sites that build array indices.

Changes

File Change
cgi-bin/var.c add _cups_parse_index(); replace 3 atoi(x)-1 index computations

Test Plan

  • Built with make (object compiles cleanly).
  • Helper logic unit-tested: normal / boundary (1, 100001, 100002) / overflow /
    non-numeric / negative / empty all return expected indices or -1.

Fixes #1748

atoi() has undefined behavior on out-of-range values. Parse the
numeric suffix of form field names with strtol(), validate errno
and the range, and return -1 on any invalid input instead.

Fixes OpenPrinting#1748
@michaelrsweet

Copy link
Copy Markdown
Member

Dupe of #1748 and #1749.

@michaelrsweet michaelrsweet self-assigned this Oct 23, 2026
@michaelrsweet michaelrsweet added the duplicate This issue or pull request already exists label Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential integer overflow when using atoi() In cgi-bin/var.c

2 participants