public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/30958] New: mkstemp always produces the same temp file if it's removed
@ 2023-10-10 14:13 sterpumihai at gmail dot com
  2023-10-10 19:05 ` [Bug libc/30958] " adhemerval.zanella at linaro dot org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: sterpumihai at gmail dot com @ 2023-10-10 14:13 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=30958

            Bug ID: 30958
           Summary: mkstemp always produces the same temp file if it's
                    removed
           Product: glibc
           Version: 2.40
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: sterpumihai at gmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

Hi,
I've actually dig this issue from binutils where the ar utility would
occasionally fail on my machine due to "ar.exe: could not create temporary file
whilst writing archive."
ar is basically using mkstemp to generate a temporary file.
When debugging I see that the same file name "stP1kAlM" is used for each ar
invocation. This was very strange to me so I wrote a small application using
mkstemp:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char template[100];

int main()
{
  strncpy(template, "stXXXXXX", 11);
  mkstemp(template);
  printf("%s\n", template);
  return 0;
}

Each time I execute this I get a different file BUT if I remove the temporary
file at each step, exactly like ar does, I get the same temporary file all the
time. For instance, if the above code is in main.c

gcc main.c
a
del stP1kAlM && a
del stP1kAlM && a
del stP1kAlM && a
...

This makes the application generate the same "stP1kAlM" file.

I've tried looking into sysdeps/posix/tempname.c, I guess the function is
try_tempname_len. I understand that it wants to avoid producing the same random
file if it can be opened but what I don't understand is how the random seed is
always the same.
I mean, the seed seems to at least come from the virtual address of a local
variable ie.

random_value v = ((uintptr_t) &v) / alignof (max_align_t);

Additional information:
Using gcc from https://github.com/brechtsanders/winlibs_mingw
Reproducing with gcc 13.2 or 10.5 32 bit on Windows 10

I really wanted to get to the bottom of this myself but I'm fairly new to GNU.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-10-13 13:49 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-10 14:13 [Bug libc/30958] New: mkstemp always produces the same temp file if it's removed sterpumihai at gmail dot com
2023-10-10 19:05 ` [Bug libc/30958] " adhemerval.zanella at linaro dot org
2023-10-11  4:09 ` sterpumihai at gmail dot com
2023-10-11 10:23 ` fweimer at redhat dot com
2023-10-11 10:23 ` sam at gentoo dot org
2023-10-11 12:45 ` adhemerval.zanella at linaro dot org
2023-10-11 14:57 ` sterpumihai at gmail dot com
2023-10-11 15:09 ` schwab@linux-m68k.org
2023-10-11 15:36 ` sterpumihai at gmail dot com
2023-10-11 15:40 ` sterpumihai at gmail dot com
2023-10-11 15:47 ` sterpumihai at gmail dot com
2023-10-11 16:09 ` adhemerval.zanella at linaro dot org
2023-10-13 12:01 ` sam at gentoo dot org
2023-10-13 13:49 ` bruno at clisp dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).