Web lists-archives.com

Re: [Mingw-users] errno_t and strerror_s




-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 03/12/16 17:02, DAVENPORT, MARC wrote:
> I found a couple relevant pages on the web. The first discusses the
> thread safety of strerror and proposes a short unit test that quickly
> fails:
> 
> http://lua-users.org/lists/lua-l/2012-03/msg00691.html

Thanks.  Do you mean quickly fails for you, when you compile and link
it with my tentative strerror_r() implementation?  Or just that the OP
(on the Lua list) suggested that it quickly failed on his OS-X box?

FWIW, I modified it to use win32threads, (instead of pthreads), and it
does *not* fail for me, (even after several hundred million iterations
within six concurrent threads).  Frankly, given MSDN's documented use
of _sys_nerr and _sys_errlist, and assuming an implementation based on
the obvious relationship between strerror() and these two, I can see
no mechanism whereby strerror() itself could be anything other than
thread safe, provided the content of _sys_errlist remains unchanged,
(even if a single instance is shared by multiple threads).  Why might
_sys_errlist change?  Perhaps as a result of a locale change, (but the
proposed test case doesn't perform one).  However, even if I include a
setlocale() call, requesting a switch to (say) French, or German, I get
nothing but English messages, returned by strerror() on my WinXP VM,
(maybe a quirk of my primarily English language installation -- even if
I set an alternative language via control panel, the entire UI remains
resolutely in English), and the test continues to run indefinitely,
without failure.

Looks to me as if my tentative strerror_r() implementation should be
fit for purpose, unless someone can devise a more rigorous test to
prove otherwise.

- -- 
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJYRIgTAAoJEMCtNsY0flo/k84P/AsO1BuT2UzbwI6aUySqRghX
o/fdryAg/1zixrIzP7v/+wSsvhUBiG48IqbYwQn7wfI4mJ/mq6WjZfHnqtzXLzdF
Dd1WReXbmsIKwbU3nlVLmjNCIvESM9fX0XM5ElU+sHCQqXS4bTMKiNZj49sStFh7
n39y8BFy/hepmPtCIzzB9rtQtdLOy0VCqqKfPuUjKagBW8q1xQbyat9ZeGA9Dhn7
Wgpv18ERH0cGhhDH/rjjp9l3FAKzbtUXP4LU3fykcCDD2eFpzVpHjYXkaVYL12r+
d3MQWZtWxhU8mSn91ehgra+hYZLU/BxTyoSdZ4jgW1EtnqEfLqJdqqtC6eEnrjNc
0Z+K58Arerf8KhUSQzPQ2dk3kx75yCYB//O8JUyyQsnsERXa+t0GRg10nBiAlQsb
xaFqk+HR1FvI+YNt7UkTuhoXk8DrGOUhYQXXvK68VzwFhVY0EGmRGODazBND4cq1
fKtn8A0B/UJsi5W5FMWhmhjmEcqP0J8yeBOoFGprsIFuYUF5kCran8cz59sEK+Me
lUfAxHEC7tosOnYowkSSdO7vJVShiJ12yli/1XN2pjCMMrMiON2N0+2LwNsXwDtM
qM4zNRYhoBfMSsJyXOsBvXYBvMqFwfMHjxd/xUys8ExIlkIg+867idCPJOmXkG8F
e/ZXGEP+4MXaIls+NsU4
=kC6Y
-----END PGP SIGNATURE-----
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include <locale.h>
#include <unistd.h>
#include <errno.h>

int strerror_r( int errnum, char *buf, size_t len )
{
  if( buf == NULL )
    return errno = ERANGE;
  if( ((unsigned)(errnum)) >= sys_nerr )
  { snprintf( buf, len, "Unknown error: %d", errnum );
    return errno = EINVAL;
  }
  if( snprintf( buf, len, "%s", strerror( errnum )) >= len )
    return errno = ERANGE;
  return 0;
}

static int report_count = 0;

void thread_loop( int num )
{
  char expected[128];
  long count = 0L;
  if( num > 3 ) setlocale( LC_ALL, "de-DE" );
  strerror_r( num, expected, sizeof(expected) );
  fprintf( stderr, "Begin thread #%d: %s\n", num, expected );
  while( 1 )
  { if( strcmp(expected, strerror(num)) )
    { fprintf(stderr, "Thread #%d failed after %d iterations\n", num, count);
      abort();
    }
    if( ((++count % 10000000) == 0) && report_count )
    { fprintf( stderr, "%3d:(%d)\t\r", num, count );
      fflush( stderr );
    }
  }
}

void thread_start( void *argp ) { thread_loop( *((int *)(argp)) ); }

int main()
{
  int num;
  for( num = 1; num < 6; ++num )
  { _beginthread( thread_start, 0, &num );
    sleep( 1 );
  }
  setlocale( LC_ALL, "de-DE" );
  report_count = 1;
  thread_loop( num );
  return 0;
}
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe