Skip to content

Commit

Permalink
Fixed bug where cf-net.exe get fails to create file
Browse files Browse the repository at this point in the history
When calling `fdopen()` in `safe_fopen_create_perms()` using the `"wx"`
mode we get...

```
warning: Invalid parameter passed to C runtime function.
```

...on Windows. Looking in both Linux man pages and Win32 API, I see that
the mode argument `"x"` is not specified as a valid argument in either
of them. Hence, it is probably not supported. Maybe Unix systems simply
ignore it, while Windows returns error? Either way, I believe it is safe
to omit it, given that we already called `open()` with the `O_EXCL`
flag, and that we are getting the file stream from that same file
descriptor.

Ticket: ENT-12511
Changelog: Title
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Dec 20, 2024
1 parent 69e2624 commit 750bef6
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion libutils/file_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,28 @@ FILE *safe_fopen_create_perms(
return NULL;
}

char fdopen_mode_str[3] = {0};
int fdopen_mode_idx = 0;

int flags = 0;
for (int c = 0; mode[c]; c++)
{
/* Ignore mode letters not supported in fdopen() for the mode string
* that we will later use in fdopen(). Mode letters like 'x' will for
* example cause fdopen() to return error on Windows. According to the
* man page fdopen(3), the mode must be one of "r", "r+", "w", "w+",
* "a", "a+". Windows also have additional characters that are
* Microsoft extensions, but advice against using them to preserve
* ANSI portability. */
if (strchr("rwa+", mode[c]) != NULL)
{
if (fdopen_mode_idx >= (sizeof(fdopen_mode_str) - 1))
{
ProgrammingError("Invalid flag for fdopen: %s", mode);
}
fdopen_mode_str[fdopen_mode_idx++] = mode[c];
}

switch (mode[c])
{
case 'r':
Expand Down Expand Up @@ -1031,7 +1050,8 @@ FILE *safe_fopen_create_perms(
{
return NULL;
}
FILE *ret = fdopen(fd, mode);

FILE *ret = fdopen(fd, fdopen_mode_str);
if (!ret)
{
close(fd);
Expand Down

0 comments on commit 750bef6

Please sign in to comment.