Skip to content

fix(native): Replace unsafe atol() with folly::to<> in PrestoExchangeSource#27223

Merged
amitkdutta merged 1 commit intoprestodb:masterfrom
amitkdutta:export-D94573576
Feb 27, 2026
Merged

fix(native): Replace unsafe atol() with folly::to<> in PrestoExchangeSource#27223
amitkdutta merged 1 commit intoprestodb:masterfrom
amitkdutta:export-D94573576

Conversation

@amitkdutta
Copy link
Copy Markdown
Contributor

@amitkdutta amitkdutta commented Feb 26, 2026

Summary:
Replace atol() calls with folly::to<> when parsing HTTP response headers
in PrestoExchangeSource. atol() silently returns 0 on empty/malformed
input and has undefined behavior on overflow. folly::to<> throws on invalid
input, making failures explicit rather than silently degrading.

Two call sites fixed:

  • Content-Length header parsing (line ~301): guarded with an empty check since
    a missing Content-Length header is valid and should default to 0.
  • Next page token header parsing (line ~334): no empty guard needed since the
    caller already checks for non-empty.
== NO RELEASE NOTE ==

Summary by Sourcery

Bug Fixes:

  • Prevent silent failures and undefined behavior when parsing Content-Length and next page token HTTP headers by using folly::to instead of atol.

…Source

Summary:
Replace `atol()` calls with `folly::to<>` when parsing HTTP response headers
in `PrestoExchangeSource`. `atol()` silently returns 0 on empty/malformed
input and has undefined behavior on overflow. `folly::to<>` throws on invalid
input, making failures explicit rather than silently degrading.

Two call sites fixed:
- Content-Length header parsing (line ~301): guarded with an empty check since
  a missing Content-Length header is valid and should default to 0.
- Next page token header parsing (line ~334): no empty guard needed since the
  caller already checks for non-empty.

Differential Revision: D94573576
@amitkdutta amitkdutta requested review from a team as code owners February 26, 2026 22:08
@prestodb-ci prestodb-ci added the from:Meta PR from Meta label Feb 26, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Feb 26, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaces unsafe uses of C’s atol() in PrestoExchangeSource HTTP header parsing with folly::to<>, adding an explicit empty-header guard for Content-Length while relying on existing non-empty checks for the next-page token header.

File-Level Changes

Change Details Files
Make Content-Length parsing safe and explicit about missing/invalid values.
  • Include folly/Conv.h to use folly::to conversions.
  • Store Content-Length header value in a local string variable for reuse and clarity.
  • Guard Content-Length parsing so an empty header maps explicitly to 0 instead of invoking conversion.
  • Use folly::to<uint64_t> to parse the Content-Length string, which throws on invalid or overflowing input.
presto-native-execution/presto_cpp/main/PrestoExchangeSource.cpp
Make next page token parsing use folly::to for safer numeric conversion.
  • Replace atol() with folly::to<int64_t> when converting the next page token header to an integer.
  • Rely on the existing caller-side non-empty check, so no additional empty-guard logic is added around the conversion.
presto-native-execution/presto_cpp/main/PrestoExchangeSource.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Now that folly::to will throw on malformed Content-Length or next-token headers, consider wrapping these conversions in a small helper that catches folly::ConversionError and rethrows with additional context (e.g., URL, header name, value) to make diagnosing bad upstream responses easier.
  • For contentLength, you special-case the empty string to 0; if there is any possibility of whitespace-only or otherwise non-canonical values, it may be safer to trim the header value before checking emptiness to avoid unexpected exceptions from folly::to.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `folly::to` will throw on malformed `Content-Length` or next-token headers, consider wrapping these conversions in a small helper that catches `folly::ConversionError` and rethrows with additional context (e.g., URL, header name, value) to make diagnosing bad upstream responses easier.
- For `contentLength`, you special-case the empty string to 0; if there is any possibility of whitespace-only or otherwise non-canonical values, it may be safer to trim the header value before checking emptiness to avoid unexpected exceptions from `folly::to`.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@amitkdutta amitkdutta merged commit 26b7228 into prestodb:master Feb 27, 2026
81 of 88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants