From a5c862029846ddd5cecf44819f2a967e2a1672a9 Mon Sep 17 00:00:00 2001 From: Stuart Bennett Date: Fri, 14 Mar 2008 11:58:33 -0400 Subject: [PATCH] Bug #13988: Fix reads from "0"-sized ROMs. --- src/linux_devmem.c | 2 +- src/linux_sysfs.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/linux_devmem.c b/src/linux_devmem.c index a68ea11..21b45ef 100644 --- a/src/linux_devmem.c +++ b/src/linux_devmem.c @@ -124,7 +124,7 @@ pci_device_linux_devmem_read_rom(struct pci_device *dev, void *buffer) else { size_t bytes; - for (bytes = 0; bytes < priv->base.rom_size; /* empty */) { + for (bytes = 0; bytes < rom_size; /* empty */) { const ssize_t got = pread(fd, buffer, rom_size - bytes, rom_base + bytes); if (got == -1) { diff --git a/src/linux_sysfs.c b/src/linux_sysfs.c index 84cdb84..873dc02 100644 --- a/src/linux_sysfs.c +++ b/src/linux_sysfs.c @@ -307,6 +307,7 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer ) int fd; struct stat st; int err = 0; + size_t rom_size; size_t total_bytes; @@ -331,6 +332,9 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer ) return errno; } + rom_size = st.st_size; + if ( rom_size == 0 ) + rom_size = 0x10000; /* This is a quirky thing on Linux. Even though the ROM and the file * for the ROM in sysfs are read-only, the string "1" must be written to @@ -340,9 +344,9 @@ pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer ) write( fd, "1", 1 ); lseek( fd, 0, SEEK_SET ); - for ( total_bytes = 0 ; total_bytes < st.st_size ; /* empty */ ) { + for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) { const int bytes = read( fd, (char *) buffer + total_bytes, - st.st_size - total_bytes ); + rom_size - total_bytes ); if ( bytes == -1 ) { err = errno; break; -- 2.11.0