pthreads in guid.c break compile

Derek Atkins warlord at MIT.EDU
Thu Jul 10 19:56:29 CDT 2003


Linas,

linas at linas.org (Linas Vepstas) writes:

> I agree with Derek, small buffers on stack are harmless.  Anything under
> a few KB on stack is harmless.  I'm not sure about glibc, but
> some implementations of pthread_getspecific are cpu-sucking. 

Thanks...

> However, the following works in C.  According to the theory
> of the C language, it should work with any compiler. 
> Note that the value of a block {...} is the value of the last statement
> of a block. Note that ({...}) is needed to get at that value. 
> 
> typedef struct { char str[GUID_ENCODING_LENGTH]; } block_t;
> block_t hack_guid_to_string(const GUID * guid) {
> 	block_t b;
> 	guid_to_string_buff(guid, &b.str[0]);
> 	return b;
> }
> 
> #define guid_to_string(g) ({block_t bl; bl=hack_guid_to_string(g); &bl.str[0]; })
[example snipped]

> The above automatically puts the string onto the stack, and 
> is completely thread-safe.  Anyone care to test & drop into CVS?

It's thread-safe, but not stack-safe.  the buffer is only on the stack
within the block.  As soon as you return from the stack you are
referencing "invalid" data, which could get overwritten at any time.

This is equivalent of the following (broken) code:

{
  char *s;
  ...
  { 
    char bar[32];
    bar_fcn(guid, bar);
    s = bar;
  }
  ...
  do_something_with_s(s);
  ...
}

s is pointing to some random place on the stack, but there is
no guarantee what that data will be -- it all depends what happens
between "s = bar" and "do_something_with_s".

> Its a little dirty-looking, but its makes the printf's use nice,
> and is otherwise just a 'deprecated' function anyway.  Its nicer than
> pthread_xxx

FWIW, I removed all the pthread_xxx, because it caused gnucash to
completely hang on my machine. :(  I just left it as a static buffer.
So, we're not thread safe, and data can be over-written if there are
multiple callers.  But it works/runs.

> --linas

-derek

-- 
       Derek Atkins, SB '93 MIT EE, SM '95 MIT Media Laboratory
       Member, MIT Student Information Processing Board  (SIPB)
       URL: http://web.mit.edu/warlord/    PP-ASEL-IA     N1NWH
       warlord at MIT.EDU                        PGP key available


More information about the gnucash-devel mailing list