]> thyrsus.com Git - semex.git/commitdiff
Cope with and warn of possible word length issues.
authorEric S. Raymond <esr@thyrsus.com>
Thu, 21 Oct 2010 08:28:36 +0000 (04:28 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 21 Oct 2010 08:28:36 +0000 (04:28 -0400)
README
semex.c

diff --git a/README b/README
index dc5f837cb65a14249f39b612f908899fec2803ee..d78ed58f72d02981c5d5ec831c0f03bb129bd20d 100644 (file)
--- 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 <http://www.catb.org/~esr/semex/>.
 
 The MIT/X license applies.
diff --git a/semex.c b/semex.c
index 4823c695d963778a894decf87e7a468703b52054..aa92ceeeb3b8c33293df0def884b3c95341648f3 100644 (file)
--- 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