Use "e" mode flag in fopen () calls for race-free setting of the close-on-exec flag

All Unix CRTs examined: glibc, musl, BSDs, Apple libc, Android bionic
ignore unknown fopen () mode flags, so this flag can be added
unconditionally for Unix builds.

Only Windows CRT is intolerant of these, so the single case in
g_dbus_address_connect () where the fopen () call is shared between Unix
and Windows needs appropriate platform-specific handling.

Skipped the call sites in libcharset and xdgmime copylibs.
This commit is contained in:
Maciej S. Szmigiero 2023-02-19 16:46:03 +01:00 committed by Philip Withnall
parent 3f2e18b07c
commit 0e7bf99ec2
3 changed files with 9 additions and 3 deletions

View File

@ -53,6 +53,12 @@
#include <windows.h>
#endif
#ifdef G_OS_WIN32
#define FO_CLOEXEC ""
#else
#define FO_CLOEXEC "e"
#endif
#include "glibintl.h"
/**
@ -711,7 +717,7 @@ g_dbus_address_connect (const gchar *address_entry,
int errsv;
/* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
f = fopen (nonce_file, "rb");
f = fopen (nonce_file, "rb" FO_CLOEXEC);
errsv = errno;
if (f == NULL)
{

View File

@ -3166,7 +3166,7 @@ _resolve_dev_root (void)
/* see if device with similar major:minor as /dev/root is mention
* in /etc/mtab (it usually is)
*/
f = fopen ("/etc/mtab", "r");
f = fopen ("/etc/mtab", "re");
if (f != NULL)
{
struct mntent *entp;

View File

@ -446,7 +446,7 @@ read_aliases (const gchar *file,
FILE *fp;
char buf[256];
fp = fopen (file,"r");
fp = fopen (file, "re");
if (!fp)
return;
while (fgets (buf, 256, fp))