9a842412301083b6e95344ac389d87bf43c90bd1
tdreszer
  Tue Aug 7 11:52:38 2012 -0700
The casting failed in the 32bit build, due to pointer size issues.  This version works in both 32 and 64 bit compiles and functionally changes nothing.  Since this is a patch request, let me be clear that no checked in code yet calls any of these functions in this new module.  The only affect this change has is that the 32bit build should now complete.
diff --git src/lib/dsPrint.c src/lib/dsPrint.c
index 33ebcc6..fd25fb3 100644
--- src/lib/dsPrint.c
+++ src/lib/dsPrint.c
@@ -1,48 +1,50 @@
 // dsPrint - Dynamic string Stack based Printing.
 //
 // This module relies upon a dyString based stack of buffers which accumulate output
 // for later printing.  As a stack, only the top buffer can be filled and flushed out
 // (dsPrintFlush).  When flushing, the top buffer's content is appended to the next
 // lower buffer, unless explicitly requested otherwise (dsPrintDirectly).
 // If the stack is empty, dsPrintf will be equivalent to printf.
 //
 // Output goes to STDOUT unless a single alternative out file is registered.
 
 #include "common.h"
 #include "dsPrint.h"
 
 // The stack of dyString buffers is only accessible locally
 static struct dyString *dsStack = NULL;
-#define dsAssertToken(token) assert((int)(long long)dsStack == token)
+// It is desirable to return a unique token but NOT give access to the dyString it is based on
+#define dsPointerToToken(pointer) (int)((long)(pointer) & 0xffffffff)
+#define dsAssertToken(token) assert(dsPointerToToken(dsStack) == token)
 
 // This module defaults to STDOUT but an alternative can be registered
 static FILE *dsOut = NULL;
 #define dsInitializeOut() {if (dsOut == NULL) dsOut = stdout;}
 #define dsAssertUnregisteredOut() assert(dsOut == stdout || dsOut == NULL)
 #define dsAssertRegisteredOut(out) assert(dsOut == out)
 
 
 int dsPrintOpen(int initialBufSize)
 // Allocate dynamic string and puts at top of stack
 // returns token to be used for later stack accounting asserts
 {
 if (initialBufSize <= 0)
     initialBufSize = 2048; // presume large
 struct dyString *ds = dyStringNew(initialBufSize);
 slAddHead(&dsStack,ds);
-return (int)(long long)ds;
+return dsPointerToToken(ds);
 }
 
 
 int dsPrintf(char *format, ...)
 // Prints into end of the top ds buffer, and return resulting string length
 // If there is no current buffer, this acts like a simple printf and returns -1
 {
 int len = -1; // caller could assert returned length > 0 to ensure dsPrint is open!
 va_list args;
 va_start(args, format);
 if (dsStack != NULL)
     {
     dyStringVaPrintf(dsStack, format, args);
     len = dyStringLen(dsStack);
     }
@@ -134,31 +136,31 @@
 int len = dyStringLen(dsStack);
 dyStringClear(dsStack);
 return len;
 }
 
 
 static int dsPrintStackIxFromToken(int token)
 // Return the stack member corresponding to the token.
 // This ix can be used to walk up the stack from a given point
 {
 struct dyString *ds = dsStack; 
 int ix = 0;
 
 for (; ds != NULL; ds = ds->next, ++ix)
     {
-    if ((int)(long long)ds == token)
+    if (dsPointerToToken(ds) == token)
         return ix;
     }
 return -1;
 }
 
 
 static struct dyString *dsPrintStackMemberFromIx(int ix)
 // Return the stack member corresponding to the ix.
 // By walking stack ix's, you can walk up or down the stack
 {
 if (ix < 0)
     return NULL;
 return slElementFromIx(dsStack,ix);
 }
 
@@ -221,54 +223,54 @@
         len += dyStringLen(ds);
     }
 return len;
 }
 
 
 int dsPrintFlushAndCloseAll()
 // flushes, frees and closes all stacked buffers
 // returns length flushed
 {
 int len = 0;
 if (dsStack != NULL)
     {
     while (dsStack != NULL)
         {
-        int token = (int)(long long)dsStack;
+        int token = dsPointerToToken(dsStack);
         len = dsPrintFlushAndClose(token); // Only the last length is needed!
         }
     }
 return len;
 }
 
 
 char *dsPrintCannibalizeAndClose(int token)
 // Closes the top stack buffer returning content.  Returned string should be freed.
 {
 dsAssertToken(token);
 struct dyString *ds = slPopHead(&dsStack);
 return dyStringCannibalize(&ds);
 }
 
 
 char *dsPrintCannibalizeAndCloseAll()
 // Closes all stack buffers and returns a single string that is the full content.  
 // Returned string should be freed.
 {
 while (dsStack != NULL && dsStack->next != NULL)
     {
-    int token = (int)(long long)dsStack;
+    int token = dsPointerToToken(dsStack);
     dsPrintFlushAndClose(token); // Flushes each to the next lower buffer
     }
 if (dsStack == NULL)
     return NULL;
 else
     return dyStringCannibalize(&dsStack);
 }
 
 /*
 void dsPrintTest()
 // Tests of dsPrint functions
 {
 printf("Plain printf\n");
 dsPrintf("1 dsPrintf unopened\n");