From: Eric S. Raymond Date: Thu, 21 Oct 2010 08:28:36 +0000 (-0400) Subject: Cope with and warn of possible word length issues. X-Git-Tag: 1.6~1 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=1f85f4bf1eb5d53d652f22fbf22b674e9d0e0da5;p=semex.git Cope with and warn of possible word length issues. --- diff --git a/README b/README index dc5f837..d78ed58 100644 --- a/README +++ b/README @@ -3,6 +3,10 @@ This was originally written sometime in the dark and backward abysm of time back in the 1980s. +If you are on a 32-but machine or using a compiler where you can't count +on a memory address being the same size as a long int, see the WARNING +comments in the code. + The latest version of this code lives at . The MIT/X license applies. diff --git a/semex.c b/semex.c index 4823c69..aa92cee 100644 --- a/semex.c +++ b/semex.c @@ -127,8 +127,17 @@ char *argv[]; nsems = 1; perms = 0660; /* read & alter by owning user & group */ semflg = 0; - - sc = sscanf(cmdline, "c %ld %d %s %o", &key,&nsems,strbuf,&perms); + long int wordlenhack; + + /* + * WARNING: wordlenhack should avoid trouble if the width of the + * key_t typedef happens not to be long int, but it is possible + * to imagine a compiler screwing this up (especially on remnant + * 32-bit machines). + */ + sc = sscanf(cmdline, "c %ld %d %s %o", + &wordlenhack, &nsems, strbuf, &perms); + key = wordlenhack; if (sc >= 3) { @@ -143,7 +152,15 @@ char *argv[]; semid = semget(key, nsems, semflg); if (tty) { - (void) printf("semget(%ld, %d, %04o) ", key, nsems, semflg); + /* + * WARNING: The long int cast could result in some + * perverse output if key is not long int; it's + * probably actually a memory address making the + * identity likely on modern compilers. But it is + * possible to imagine a compiler screwing this up + * (especially on remnant 32-bit machines) + */ + (void) printf("semget(%ld, %d, %04o) ", (long int)key, nsems, semflg); if (semid == -1) (void) printf("failed: %s\n", strerror(errno)); else