Skip to content

Commit f369b66

Browse files
committed
make sure /del /log:n parameter matches n-th displayed log. part.
1 parent 5980158 commit f369b66

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

doc/fdisk/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Version 1.4.0 (2025-01-16)
1313
Fixes:
1414
- CRITICAL: FDISK would wipe the disk if a logical other the first is
1515
deleted while there are 23 logical partitions defined for the disk.
16+
- CRITICAL: The number n for the command line operations
17+
/del /log:n and /del /num:n did not always reflect the n-th
18+
logical partition shown to the user via /info. This could lead the
19+
user to delete wrong partitions.
1620
- HIGH: If there are multiple gaps between the partitions, FDISK would
1721
not find the largest free space but the first.
1822
Changes:

source/fdisk/cmd.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,31 @@ void Command_Line_Create_Primary_Partition( void )
254254
Shift_Command_Line_Options( option_count );
255255
}
256256

257+
static int Nth_Log_Part_Defined( Partition_Table *pDrive, int num )
258+
{
259+
int i, valid_parts = 0;
260+
261+
for ( i = 0; i < MAX_LOGICAL_DRIVES; i++ ) {
262+
if ( pDrive->log_drive[i].num_type == 0 ) {
263+
continue;
264+
}
265+
266+
if ( valid_parts == num ) {
267+
return i;
268+
}
269+
270+
valid_parts++;
271+
}
272+
273+
return i;
274+
}
275+
257276
/* /DELETE command line option */
258277
void Command_Line_Delete( void )
259278
{
260279
Partition_Table *pDrive = &part_table[flags.drive_number - 0x80];
261280
int error_code = 0;
281+
int part_num;
262282

263283
/* Delete the primary partition */
264284
if ( 0 == strcmp( arg[1].choice, "PRI" ) ) {
@@ -315,8 +335,9 @@ void Command_Line_Delete( void )
315335

316336
/* Delete a Logical DOS Drive */
317337
else if ( 0 == strcmp( arg[1].choice, "LOG" ) ) {
318-
if ( ( arg[1].value >= 1 ) && ( arg[1].value <= 23 ) ) {
319-
error_code = Delete_Logical_Drive( (int)( arg[1].value - 1 ) );
338+
if ( ( arg[1].value >= 1 ) && ( arg[1].value <= MAX_LOGICAL_DRIVES ) &&
339+
( ( part_num = Nth_Log_Part_Defined( pDrive, arg[1].value - 1 ) ) < MAX_LOGICAL_DRIVES ) ) {
340+
error_code = Delete_Logical_Drive( part_num );
320341
}
321342
else {
322343
/* NLS:Logical drive number %d is out of range. */
@@ -330,8 +351,9 @@ void Command_Line_Delete( void )
330351
if ( ( arg[1].value >= 1 ) && ( arg[1].value <= 4 ) ) {
331352
error_code = Delete_Primary_Partition( (int)( arg[1].value - 1 ) );
332353
}
333-
else if ( ( arg[1].value >= 5 ) && ( arg[1].value <= 28 ) ) {
334-
error_code = Delete_Logical_Drive( (int)( arg[1].value - 5 ) );
354+
else if ( ( arg[1].value >= 5 ) && ( arg[1].value <= 28 ) &&
355+
( ( part_num = Nth_Log_Part_Defined( pDrive, arg[1].value - 5 ) ) < MAX_LOGICAL_DRIVES ) ) {
356+
error_code = Delete_Logical_Drive( (int)( part_num ) );
335357
}
336358
else {
337359
/* NLS:Partition number is out of range. */

source/fdisk/pcompute.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ int Delete_Logical_Drive( int logical_drive_number )
315315
if ( !pDrive->usable || !pDrive->ext_usable ) {
316316
return 99;
317317
}
318-
319318
return Delete_EMBR_Chain_Node( pDrive, logical_drive_number );
320319
}
321320

0 commit comments

Comments
 (0)