Segfault fix in scsi_id-0.6
[following up on hotplug-devel ...]
Quoted 23 lines · Andrew Patterson
Hi Patrick,
I found a segfault when running
scsi_idon an IA-64 box. I assume you will get the same behavior on any box where an int is 32 bits and not
64. I have included a patch to fix the problem. Note the "- (char *)0 change that I stole from lmdd.Andrew
diff -Nur
scsi_id-0.6/scsi_id.cscsi_id-0.6.new/scsi_id.cscsi_id.c 2004-09-28 15:24:44.000000000 -0600 renamed from scsi_id.c 2004-07-30 14:33:59.000000000 -0600 +1
-2@@ -713,7 +713,7 @@ #define ALIGN 512 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN); - serial = (char*) (((int) unaligned_buf + (ALIGN - 1)) & ~(ALIGN - 1)); + serial = (char*) (((unaligned_buf - (char *)0) + (ALIGN - 1)) & ~(ALIGN - 1)); dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf,
Quoted 1 line
serial); #undef ALIGN
Thanks for pointing this out, patch works fine.
But how about just casting to unsigned long instead? Like:
Index: scsi_id.c
=================================RCS file: /home/patman/CVS/scsi_id/scsi_id/scsi_id.c,v retrieving revision 1.36
scsi_id.c +2 -1
@@ -713,7 +713,8 @@#define ALIGN 512 unaligned_buf =malloc (MAX_SERIAL_LEN +ALIGN ); - serial = (char *) (((int ) unaligned_buf + (ALIGN -1 )) & ~(ALIGN -1 )); + serial = (char *) (((unsigned long ) unaligned_buf + (ALIGN -1 )) + & ~(ALIGN -1 ));dprintf ("buffer unaligned 0x%p; aligned 0x%p\n" , unaligned_buf, serial);#undef ALIGN
-- Patrick Mansfield
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 4 lines · Patrick Mansfield
Quoted 2 lines · Andrew Patterson
+ serial = (char*) (((
unaligned_buf- (char *)0) + (ALIGN - 1)) &
~(ALIGN - 1));
Quick comment: the C compiler will treat the (char*)0 as a null pointer constant. C makes no guarantee that the resulting value will have anything to do with address 0. I believe there are architectures where a null pointer has all-bits-on or some other non-zero representation, though I don't know if Linux has ever been made to run on any of them.
scsi_id.c line 717
Quoted 4 lines
But how about just casting to unsigned long instead? Like:
scsi_id.c +2
-0+ serial = (char *) (((unsigned long ) unaligned_buf + (ALIGN -1 )) + & ~(ALIGN -1 ));
None of this pointer manipulation is technically defined to work, but from a practical standpoint this will probably be safe. There's still the possibility that an unsigned long can't hold all pointer values. If you're willing to rely on a C99 construct, you could include <stdint.h> and use uintptr_t instead.
-Dave Dodge
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
Quoted 13 lines · Dave Dodge
Quoted 4 lines · Patrick Mansfield
Quoted 2 lines · Andrew Patterson
+ serial = (char*) (((
unaligned_buf- (char *)0) + (ALIGN - 1)) &
~(ALIGN - 1));Quick comment: the C compiler will treat the (char*)0 as a null pointer constant. C makes no guarantee that the resulting value will have anything to do with address 0. I believe there are architectures where a null pointer has all-bits-on or some other non-zero representation, though I don't know if Linux has ever been made to run on any of them.
Quoted 5 lines
scsi_id.c line 717
Quoted 4 lines
But how about just casting to unsigned long instead? Like:
scsi_id.c +2
-0+ serial = (char *) (((unsigned long ) unaligned_buf + (ALIGN -1 )) + & ~(ALIGN -1 ));
This works on i386 and ia64. As Dave points out, it may not work on architectures where a pointer size != unsigned long.
Andrew Patterson
Quoted 8 lines
None of this pointer manipulation is technically defined to work, but from a practical standpoint this will probably be safe. There's still the possibility that an unsigned long can't hold all pointer values. If you're willing to rely on a C99 construct, you could include <
stdint.h> and useuintptr_tinstead.-Dave Dodge