memcached digest, Vol 1 #120 - 1 msg

Jason Titus jtitus@postini.com
Thu, 26 Feb 2004 13:02:49 -0800


>Date: Wed, 25 Feb 2004 16:28:03 +0200
>From: Anatoly Vorobey <mellon@pobox.com>
>To: memcached@lists.danga.com
>Subject: Re: Large memory support
>
>On Fri, Feb 20, 2004 at 11:21:06AM -0800, Jason Titus wrote:
>  
>
>>-        pos += sprintf(pos, "STAT limit_maxbytes %u\r\n", settings.maxbytes);
>>+        pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", settings.maxbytes);
>>    
>>
>
>This is wrong, because %llu will always expect a 64bit argument, but on
>32bit architectures your settings.maxbytes, being a size_t, will be a
>32bit argument.
>                                                                                
>There're two ways to make it right:
>                                                                                
>a) use the 'z' field width option to sprintf here (and any other place
>where we might need to output/input a size_t field) and retain the
>size_t type. However, I don't know how standard 'z' is and would
>appreciate information about it.
>                                                                                
>b) use 'unsigned long' instead of size_t, and give %l to sprintf.
>"unsigned long" works great because it's 32bit on 32bit architectures
>and 64bit on 64bit architectures.
>
>I prefer b) myself, but I may be underappreciating size_t, I'm not sure.
>
>  
>
How about just casting it as a unsigned long long? 

sprintf(pos, "STAT limit_maxbytes %llu\r\n",
        (unsigned long long)settings.maxbytes);

As for the whole size_t thing, it seems like there is some debate on 
whether it makes sense to use it everywhere.  Some platforms seem to do 
odd things with it (I gather that HPUX is one example), and you need to 
have C99 support.  On Linux size_t is always an unsigned int on 32 bit 
platforms and always an unsigned long on 64 bit platforms (at least for 
the 18 processor types supported as of 4.2.40).  Unsigned longs should 
work in most UNIXs, but is 32 bits on 64 bit Windows.  Not sure if 
anyone cares now, but I'd bet that Win64 will be a popular platform in a 
year or two.

>>-            settings.maxbytes = atoi(optarg)*1024*1024;
>>+            settings.maxbytes = (size_t) atoi(optarg)* (size_t) 1024* (size_t) 1024;
>>    
>>
>
>Most or all of these explicit typecasts shouldn't be necessary.
>
>  
>
Yeah.  Ditch those.

Jason