Revision: 45691
http://sourceforge.net/p/vice-emu/code/45691
Author: rhialto
Date: 2025-06-01 13:21:15 +0000 (Sun, 01 Jun 2025)
Log Message:
-----------
c1541: fix segfault when not attaching disk image.
When a disk image is attached, vdrive_device_setup() is called to set up the
vdrive struct. This includes memory for the disk error string buffer.
If you do a "c1541 -write foo bar" command (so without attaching a disk image),
c1541 wants to write a DRIVE NOT READY to the error string buffer. This
segfaults because it has not been allocated.
Fix: Call vdrive_device_setup() right after a vdrive is allocated, so it is
always good to use. Also, when freeing the buffers in vdrive_device_shutdown(),
set them to NULL to make sure they are not re-used accidentally.
When vdrive_device_setup() is called again for attaching a disk image, be
paranoid and first call vdrive_device_shutdown() on it.
When detaching an image, call vdrive_device_shutdown() even if no image was
attached. This prevents memory leaks.
Also: replace some 8's by DRIVE_UNIT_MIN.
Modified Paths:
--------------
trunk/vice/src/c1541.c
trunk/vice/src/vdrive/vdrive.c
Modified: trunk/vice/src/c1541.c
===================================================================
--- trunk/vice/src/c1541.c 2025-05-22 19:03:56 UTC (rev 45690)
+++ trunk/vice/src/c1541.c 2025-06-01 13:21:15 UTC (rev 45691)
@@ -252,7 +252,7 @@
return NULL;
}
- return drives[unit - 8];
+ return drives[unit - DRIVE_UNIT_MIN];
}
/* ------------------------------------------------------------------------- */
@@ -1089,6 +1089,7 @@
return -1;
}
+ vdrive_device_shutdown(vdrive);
vdrive_device_setup(vdrive, unit);
vdrive_attach_image(image, unit, 0, vdrive);
return 0;
@@ -1117,9 +1118,10 @@
disk_image_media_destroy(image);
disk_image_destroy(image);
vdrive->image = NULL;
- /* also clean up buffer used by the vdrive */
- vdrive_device_shutdown(vdrive);
}
+
+ /* also clean up buffer used by the vdrive */
+ vdrive_device_shutdown(vdrive);
}
/** \brief Open image or create a new one
@@ -5220,7 +5222,7 @@
return FD_NOTREADY;
}
- printf("validating in unit %d ...\n", dnr + 8);
+ printf("validating in unit %d ...\n", dnr + DRIVE_UNIT_MIN);
vdrive_command_validate(drives[dnr]);
return FD_OK;
@@ -5363,10 +5365,10 @@
}
if (dest_name == (char *)finfo->name) {
- printf("writing file `%s' to unit %d\n", finfo->name, dnr + 8);
+ printf("writing file `%s' to unit %d\n", finfo->name, dnr + DRIVE_UNIT_MIN);
} else {
printf("writing file `%s' as `%s' to unit %d\n", finfo->name,
- dest_name, dnr + 8);
+ dest_name, dnr + DRIVE_UNIT_MIN);
}
if (rel_record_length == 0) {
@@ -5630,6 +5632,7 @@
for (i = 0; i < NUM_DISK_UNITS; i++) {
drives[i] = lib_calloc(1, sizeof *drives[i]);
+ vdrive_device_setup(drives[i], DRIVE_UNIT_MIN + i);
}
/* The first arguments without leading `-' are interpreted as disk images
@@ -5638,10 +5641,10 @@
if ((i - 1) == NUM_DISK_UNITS) {
fprintf(stderr, "Ignoring disk image `%s'\n", argv[i]);
} else {
- if (open_disk_image(drives[i - 1], argv[i], (unsigned int)(i - 1 + 8)) != 0) {
+ if (open_disk_image(drives[i - 1], argv[i], (unsigned int)(i - 1 + DRIVE_UNIT_MIN)) != 0) {
/* error: clean up and exit */
while (--i >= 1) {
- close_disk_image(drives[i - 1], (unsigned int)(i - 1 + 8));
+ close_disk_image(drives[i - 1], (unsigned int)(i - 1 + DRIVE_UNIT_MIN));
}
for (i = 0; i < NUM_DISK_UNITS; i++) {
lib_free(drives[i]);
@@ -5764,7 +5767,7 @@
/* free memory used by the virtual drives */
for (i = 0; i < NUM_DISK_UNITS; i++) {
if (drives[i]) {
- close_disk_image(drives[i], i + 8);
+ close_disk_image(drives[i], i + DRIVE_UNIT_MIN);
lib_free(drives[i]);
}
}
@@ -5811,7 +5814,7 @@
if (check_drive_unit(dnr) < 0) {
return FD_BADDEV;
}
- dnr -= 8;
+ dnr -= DRIVE_UNIT_MIN;
}
p00save[dnr] = (unsigned int)enable;
Modified: trunk/vice/src/vdrive/vdrive.c
===================================================================
--- trunk/vice/src/vdrive/vdrive.c 2025-05-22 19:03:56 UTC (rev 45690)
+++ trunk/vice/src/vdrive/vdrive.c 2025-06-01 13:21:15 UTC (rev 45691)
@@ -176,6 +176,7 @@
p = &(vdrive->buffers[i]);
vdrive_free_buffer(p);
lib_free(p->buffer);
+ p->buffer = NULL;
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|