Skip to content

Comments

part: Check partition parsed partition type before setting it#1168

Open
vojtechtrefny wants to merge 1 commit intostoraged-project:masterfrom
vojtechtrefny:master_check-parsed-part-type
Open

part: Check partition parsed partition type before setting it#1168
vojtechtrefny wants to merge 1 commit intostoraged-project:masterfrom
vojtechtrefny:master_check-parsed-part-type

Conversation

@vojtechtrefny
Copy link
Member

@vojtechtrefny vojtechtrefny commented Feb 19, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Improved partition-type validation to detect and reject invalid numeric IDs and unknown partition types with clearer error messages, preventing invalid changes.
  • Tests

    • Added tests covering additional invalid partition ID inputs to ensure validation catches malformed and out-of-range values.

@coderabbitai
Copy link

coderabbitai bot commented Feb 19, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

Added stricter validation in set_part_type(): numeric parsing now verifies full-string consumption and rejects unknown MBR partition types; resources are released on validation failure. Also expanded tests with three invalid partition-id cases to assert error handling.

Changes

Cohort / File(s) Summary
Partition Type Validation
src/plugins/part.c
Added endptr-based numeric parsing check (uses g_ascii_strtoull and requires endptr at string terminator). After parsing, added fdisk_parttype_is_unknown() check to reject unknown MBR types; on failure unreferences ptype and pa and returns FALSE with an INVAL error.
Test Coverage Expansion
tests/part_test.py
Added three invalid partition-id test cases ("83;id", "0xfff", "999") to PartSetIdCase to ensure invalid strings raise GLib.GError and are rejected.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding validation to check the parsed partition type before setting it in the partition module.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/plugins/part.c`:
- Around line 1775-1781: The early-return path after calling
fdisk_parttype_is_unknown(ptype) leaks the parsed parttype; before returning
FALSE you must unref the allocated ptype by calling fdisk_unref_parttype(ptype)
(similar to the other early-return paths), so update the block that currently
calls fdisk_unref_partition(pa) and returns to also call
fdisk_unref_parttype(ptype) to free the resource while still setting the error
using type_str/BD_PART_ERROR_INVAL.
- Around line 1775-1781: The fdisk_parttype_is_unknown check is being applied
unconditionally and thus rejects structurally-valid custom GPT GUIDs; restrict
this validation to MSDOS/MBR only by moving or wrapping the
fdisk_parttype_is_unknown call (the block that sets BD_PART_ERROR_INVAL and
returns FALSE) so it only runs when table_type == BD_PART_TABLE_MSDOS (the same
scope where fdisk_label_parse_parttype is used for MBR); ensure
bd_part_set_part_type continues to accept unknown GPT parttypes parsed by
fdisk_label_parse_parttype and does not trigger the invalid-type error for GPT.

In `@tests/part_test.py`:
- Around line 1315-1318: The test using
BlockDev.part_set_part_id(self.loop_devs[0], ps.path, "83;id") is unreliable
because "83;id" can be parsed as decimal 83 (0x53) and may be present in
libfdisk’s MBR table; change the input to a value that cannot be interpreted as
a single-byte type such as "0x83;id" (or otherwise replace with an out-of-range
token like "0xfff") so fdisk_label_parse_parttype() will fail consistently and
the with self.assertRaises(GLib.GError): assertion around
BlockDev.part_set_part_id will reliably trigger; update or add a short comment
next to the test explaining the rationale.

@vojtechtrefny vojtechtrefny force-pushed the master_check-parsed-part-type branch from 0e6cd37 to 6e63d2c Compare February 19, 2026 14:45
@vojtechtrefny vojtechtrefny force-pushed the master_check-parsed-part-type branch from 6e63d2c to a42600f Compare February 19, 2026 14:47
Copy link
Member

@tbzatek tbzatek left a comment

Choose a reason for hiding this comment

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

Converting type_str to int back and forth is kinda nasty, but would require deeper refactoring. Good for now though.

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