Re: [patch] File locking



On Wed, Jul 03, 2002 at 04:19:29PM -0000, Morten Welinder wrote:

   +          char lockstr[300];
   +          char hostname[256];
   +          /* Create the locking string, format is 
   +           * hostname:pid */
   +          gethostname(hostname, 255);
   +          snprintf(lockstr, 300, "%s:%d", hostname, getpid());

No, array[constant], please.  Use dynamically allocated strings
instead.  Also, getpid returns a pid_t, not an int, so it does not
match "%d".


Note these arrays[constant] are only used to generate the locking string,
which is then consequently written to file. After that the arrays will
disappear off the stack. In theory it should be faster than dynamic
allocation, and buffer overflows are only possible if a) the hostname
is very large (though it can't exceed 255 bytes due to the call to
gethostname() being constrained) and b) the result of the getpid()
conversion is larger than > 44 bytes. Which frankly is ludicrous since
that implies a pid larger than 10^44 ..

Also, about getpid(); How on earth does one write the output getpid()
then? The way I understood it, the result of getpid() fits into an int
or at most, a long in most architectures. The man page for getpid() is
not exactly helpful either..

Regards,
Floris Kraak
-- 
Early to rise, early to bed, makes a man healthy, wealthy and dead.
                -- Terry Pratchett, "The Light Fantastic"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]