Skip to content

[#1727] Reject integers outside int range in as_int - #1728

Open
Keshavkumar04 wants to merge 1 commit into
pgmoneta:mainfrom
Keshavkumar04:range-check-as-int
Open

Keshavkumar04 wants to merge 1 commit into
pgmoneta:mainfrom
Keshavkumar04:range-check-as-int

Conversation

@Keshavkumar04

Copy link
Copy Markdown
Contributor

as_int checks ERANGE, which only covers long range, then narrows to int unchecked, so values between INT_MAX and LONG_MAX are accepted and truncated. -2147483649 arrives as 2147483647, which defeats callers that validate with a negative check, including as_retention_value.

as_bytes and as_seconds both bound against INT_MAX; this adds the same check to as_int.

The new test fails on unfixed code and passes with the fix.

Closes #1727

@Keshavkumar04

Copy link
Copy Markdown
Contributor Author

static-analysis fails on main too. I built cppcheck 2.22.0 as CI does and ran the baseline check against a clean main checkout:

nullPointerOutOfMemory:src/libpgmoneta/art.c: 17 in the baseline, 18 now

This branch's output is identical to main's, and configuration.c has no findings. The baseline was last regenerated for 2.19 while CI now builds 2.22.0, which reports one more in art.c.

They look like genuine unchecked mallocs (pgmoneta_art_create, pgmoneta_art_iterator_create and a few others), same class as #1590. Is art.c already covered by that work, or would a PR help?

@ecleon26

Copy link
Copy Markdown
Collaborator

@Keshavkumar04 use MCTF_ASSERT_INT_EQ(config->metrics, 0, ...) instead of >= 0 in test, so we check that the default is kept.

Otherwise it looks good.

@Keshavkumar04

Copy link
Copy Markdown
Contributor Author

@Keshavkumar04 use MCTF_ASSERT_INT_EQ(config->metrics, 0, ...) instead of >= 0 in test, so we check that the default is kept.

Otherwise it looks good.

Done. metrics isn't set in init_main_configuration and the shmem is MAP_ANONYMOUS, so 0 is the real default. Still fails on unfixed code.

Unrelated: static-analysis is red on main too, nullPointerOutOfMemory:src/libpgmoneta/art.c is 18 against a baseline of 17, since the baseline was regenerated for cppcheck 2.19 and CI now builds 2.22.0.

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me. I ran the new test on top of this PR's parent: it fails at test_configuration.c:1337 (the -2147483649 value comes back as a positive level), and passes with the change.

The same narrowing is in the sibling projects, which have their own copy of this function: pgagroal_as_int() in pgagroal's src/libpgagroal/configuration.c and as_int() in pgexporter's src/libpgexporter/configuration.c both check only ERANGE and then cast with (int)val. I'll open issues there so it gets cross-ported.

@Keshavkumar04

Copy link
Copy Markdown
Contributor Author

Looks good to me. I ran the new test on top of this PR's parent: it fails at test_configuration.c:1337 (the -2147483649 value comes back as a positive level), and passes with the change.

The same narrowing is in the sibling projects, which have their own copy of this function: pgagroal_as_int() in pgagroal's src/libpgagroal/configuration.c and as_int() in pgexporter's src/libpgexporter/configuration.c both check only ERANGE and then cast with (int)val. I'll open issues there so it gets cross-ported.

Thanks for checking the siblings. I've got both ports ready, pgexporter's as_int is identical so the same fix and test apply, and in pgagroal the truncated value walks straight through the i_value >= 0 check in as_bytes. PRs on #709 and #1118 shortly.

@ecleon26

ecleon26 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

@Keshavkumar04 Please, rebase

The ERANGE check covers long, so a value between INT_MAX and LONG_MAX was
accepted and then narrowed: 2147483648 became -2147483648 and -2147483649
became INT_MAX, defeating the callers that validate with a negative check.

Signed-off-by: Keshav Kumar <[email protected]>
@Keshavkumar04

Copy link
Copy Markdown
Contributor Author

@Keshavkumar04 Please, rebase

Rebased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as_int accepts integers outside int range and truncates them

3 participants