[parted-devel] bug? mac.c's mac_write modifies its *disk parameter

Jim Meyering jim at meyering.net
Mon Mar 5 09:51:16 CET 2007


I'm preparing a patch to add the "const" attribute where appropriate,
starting with libparted/labels/dos.c.  This went fine (albeit
tediously) up until the "msdos_write" function, which is tied to the
_PedDiskOps.write member:

    int (*write) (PedDisk* disk);

Of course, like any write function, it should have a prototype,
indicating that it treats its parameter as read-only:

    int (*write) (const PedDisk* disk);

so I changed it.  That change cascades to every other libparted/labels/*.c
file, since they all have a similar FOO_write function.  And in many
of those other files, the FOO_write function called some other static
function with a non-const-but-should-be-const *disk, so I updated
those, too.

This process seems to have exposed a logic error.  Unlike every other
FOO_write function, mac.c's mac_write function, calls the
"*disk"-modifying ped_disk_add_partition:

static int
mac_write (PedDisk* disk)
{
	if (!ped_disk_get_partition (disk, mac_disk_data->part_map_entry_num)) {
		if (!_disk_add_part_map_entry (disk, 1))
			goto error;
	}

The _disk_add_part_map_entry function also modified "*disk",
so it doesn't belong here either.

For now, I've isolated mac.c with a single cast:

	write:			(int (*) (const PedDisk*)) mac_write,

I.e., I am taking the conservative approach for now and leaving
mac.c otherwise unchanged.

Can anyone say if there's a good reason to make such an exception?



More information about the parted-devel mailing list