Skip to content

Commit

Permalink
Fix parsing of partition IDs
Browse files Browse the repository at this point in the history
Partition IDs are unsigned 64-bit integers, but were being parsed as signed integers, so were out of range if the first bit was set.
  • Loading branch information
will-v-pi committed Nov 7, 2024
1 parent a86abb7 commit 0f9977e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,18 @@ namespace cli {
base = 2;
}
try {
lvalue = std::stoll(value, &pos, base);
if (std::is_signed<T>()) {
lvalue = std::stoll(value, &pos, base);
} else {
lvalue = std::stoull(value, &pos, base);
}
if (pos != value.length()) {
return "Garbage after integer value: " + value.substr(pos);
}
} catch (std::invalid_argument&) {
return value + " is not a valid integer";
} catch (std::out_of_range&) {
return value + " is out of range";
}
if (lvalue != (int64_t)lvalue) {
return value + " is too big";
Expand Down
5 changes: 4 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5774,7 +5774,10 @@ bool partition_create_command::execute(device_map &devices) {
new_p.flags |= (link_value << PICOBIN_PARTITION_FLAGS_LINK_VALUE_LSB) & PICOBIN_PARTITION_FLAGS_LINK_VALUE_BITS;
}
if (p.contains("name")) { new_p.name = p["name"]; new_p.flags |= PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS; }
if (p.contains("id")) { get_json_int(p["id"], new_p.id); new_p.flags |= PICOBIN_PARTITION_FLAGS_HAS_ID_BITS; }
if (p.contains("id")) {
if (get_json_int(p["id"], new_p.id)) {new_p.flags |= PICOBIN_PARTITION_FLAGS_HAS_ID_BITS;}
else {string p_id = p["id"]; fail(ERROR_INCOMPATIBLE, "Partition ID \"%s\" is not a valid 64bit integer\n", p_id.c_str());}
}

if(p.contains("no_reboot_on_uf2_download")) new_p.flags |= PICOBIN_PARTITION_FLAGS_UF2_DOWNLOAD_NO_REBOOT_BITS;
if(p.contains("ab_non_bootable_owner_affinity")) new_p.flags |= PICOBIN_PARTITION_FLAGS_UF2_DOWNLOAD_AB_NON_BOOTABLE_OWNER_AFFINITY;
Expand Down

0 comments on commit 0f9977e

Please sign in to comment.