Skip to content

util: Fix upper bound in parseUIntNoThrow#2349

Open
cuiweixie wants to merge 1 commit into
aria2:masterfrom
cuiweixie:fix-parseUIntNoThrow-upper-bound
Open

util: Fix upper bound in parseUIntNoThrow#2349
cuiweixie wants to merge 1 commit into
aria2:masterfrom
cuiweixie:fix-parseUIntNoThrow-upper-bound

Conversation

@cuiweixie

Copy link
Copy Markdown

parseUIntNoThrow writes to uint32_t but compared against std::numeric_limits<int32_t>::max(), rejecting valid values in (INT32_MAX, UINT32_MAX]. Compare against uint32_t max instead.

@adonais

adonais commented Mar 29, 2026

Copy link
Copy Markdown

This patch doesn't work, modify it like this:

 bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base)
 {
-  long int t;
-  if (parseLong(t, strtol, s, base) && t >= 0 &&
-      t <= std::numeric_limits<int32_t>::max()) {
+  unsigned long t;
+  if (parseLong(t, strtoul, s, base) && t >= 0 &&
+      t <= static_cast<unsigned long>(std::numeric_limits<uint32_t>::max())) {
     res = t;
     return true;
   }

Use strtoul and unsigned long so parsing matches uint32_t semantics,
and cap the result at UINT32_MAX. The previous strtol path could not
represent the full uint32_t range correctly on LP64 platforms.
@cuiweixie cuiweixie force-pushed the fix-parseUIntNoThrow-upper-bound branch from 9a35f45 to ce82c20 Compare March 29, 2026 07:51
@cuiweixie

Copy link
Copy Markdown
Author

This patch doesn't work, modify it like this:

 bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base)
 {
-  long int t;
-  if (parseLong(t, strtol, s, base) && t >= 0 &&
-      t <= std::numeric_limits<int32_t>::max()) {
+  unsigned long t;
+  if (parseLong(t, strtoul, s, base) && t >= 0 &&
+      t <= static_cast<unsigned long>(std::numeric_limits<uint32_t>::max())) {
     res = t;
     return true;
   }

done

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.

2 participants