Re: Trouble with MinGW's mkstemp

Keith Marshall <[email protected]>
Newsgroups gmane.comp.gnu.mingw.user
Organization MinGW.org Project
Message-ID <[email protected]>
On 11/01/19 16:06, Eli Zaretskii wrote:
> The simple test program below almost always fails on my system.
> "Almost always" means that it succeeds to create and open a temporary
> file once every 6 invocations, on the average, and fails for all the
> rest.  When it fails, the value of errno is mostly EINVAL, but on a
> couple of occasions I saw ENOENT.
> 
> If I step with a debugger into __mingw_mkstemp, I see that
> __mingw_crypto_tmpname in most cases generates a file name that is
> invalid on Windows filesystems: it includes characters whose value is
> below 32,

Which isn't *supposed* to happen; the six 'X' chars, at the end of the
template, are supposed to be replaced by random choices from within the
regex pattern [0-9a-z].

> This is with the latest MinGW runtime 5.2 on a Windows XP system, with
> MinGW GCC 7.3.0 and Binutils 2.31.1.
> 
> Did I miss something obvious, or is this a real bug?

Thanks.  It is a real bug, introduced in mingwrt-5.2, while attempting
to avoid a char * vs. unsigned char * type mismatch warning; the
simplest change was, unfortunately, the wrong choice.  The attached
patch should fix it.

-- 
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F

_______________________________________________
MinGW-Users mailing list
[email protected]

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.osdn.me/mailman/listinfo/mingw-users
Also: mailto:[email protected]?subject=unsubscribe
mkstemp-file-name-generator.patch (text/x-patch, 2.3 KB)
# HG changeset patch
# Parent 0629796335f4aa1f344561beadaa705ddd22f197
Fix mkstemp() file name generator defect.

* mingwex/cryptnam.c (crypto_random_filename_char): Revert 2018-09-06
change; argument type must be unsigned char *, to avoid math errors.
(__mingw_crypto_tmpname): Use explicit casts to resolve type conflicts.

diff --git a/mingwrt/mingwex/cryptnam.c b/mingwrt/mingwex/cryptnam.c
--- a/mingwrt/mingwex/cryptnam.c
+++ b/mingwrt/mingwex/cryptnam.c
@@ -8,11 +8,11 @@
  * the mkstemp(3) and mkdtemp(3) functions, respectively.
  *
  * $Id$
  *
  * Written by Keith Marshall  <[email protected]>
- * Copyright (C) 2013, 2014, 2018, MinGW.org Project.
+ * Copyright (C) 2013, 2014, 2018, 2019, MinGW.org Project.
  *
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
@@ -92,11 +92,12 @@ CRYPTO_INLINE void *crypto_randomize( vo
   /* ...or nothing, otherwise.
    */
   return NULL;
 }
 
-CRYPTO_INLINE char *crypto_random_filename_char( char *caret )
+CRYPTO_INLINE
+unsigned char *crypto_random_filename_char( unsigned char *caret )
 {
   /* Helper to generate a random sequence of characters, suitable for
    * use in file names; although there are other valid possibilities, we
    * restrict this to the set of lower case ASCII alpha-numerics, giving
    * us 36 degrees of freedom for each character; (note that we cannot
@@ -153,16 +154,16 @@ char *__mingw_crypto_tmpname( char *temp
    * per template per process thread.
    *
    * We begin by locating the position, within the given template,
    * where the string of six replaceable 'XXXXXX's should begin.
    */
-  char *tail = template + strlen( template ) - 6;
+  unsigned char *tail = (unsigned char *)(template) + strlen( template ) - 6;
 
   /* Provided this appears sane -- i.e. it at least doesn't place the
    * six character "tail" before the start of the template itself...
    */
-  if( tail >= template )
+  if( (char *)(tail) >= template )
   {
     /* ...then, walk over each of the six bytes of the "tail", until
      * we reach the NUL terminator...
      */
     while( *tail )
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJcOP8cAAoJEMCtNsY0flo/XAgQAJzGOdlcV7Up/aAm5h5FHsz6
gycG8F4u1unBr2J3w0217aP3eKHq/ArjOx8SSfdFHkVIp7Xml9aNG3wFCZW/+dQz
+1LOLY/y+EYccNFg8bN2YUrTnZTlSE3ASJJ3P0Hmjm/1KVu59XSZSm5/SuQ/WJKj
4zDM8VAjDAbm9/+7B4QI+rCWsFm4KIe2WTnDTxiF+wgocn9/wwKkJfeQLYuFfS4e
goIf6579XfbjR/vkbHnyeYxn9Lnpdi12DU/eQy31QNrFU4fzp5wJwzKGnq1ZIUhS
mEPgVX8/yyoS4oIKPn5xgM+VX6ZJLfa2tWJYxY8H/smieaVNamAylqiUw49X8Blq
gcaQE59qCCv3OKpi8K/MbTLKRL0NswbGA/ezrWFvEyaLC6oJuUrjGH0o1PaHIKHt
LHtDOp/1dzpWiAA4eUuEu5J7pRG2GSr1YcJCUoC6xc+Y39M8/X3kaEcXIIwZhkqh
jtwqKWiwnRAcniX+Muh2Oxx/hIWPCR1bshuRvyYiCbLwelmBDlCAL9LpxyDPvElU
ZH5o2cCmruw0RtFTDnadDaft+UK0H5MN+2aUvMWEJefNrMCU5BqiO/Hpn7vS1TMM
enSJcaOq9XG0vfzdmq21y9CDHR5OUo6WQ+j8vc/WY/JbBuDuaycHPrJ4fNtPIJLZ
ztXBgqlsnxkgEvGgisfv
=7e/z
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.