Skip to content

Commit 9c32d19

Browse files
committed
fmemopen: set errno as required by POSIX
This fixes quite a few of the NetBSD fmemopen tests.
1 parent e869838 commit 9c32d19

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

newlib/libc/tinystdio/fmemopen.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "stdio_private.h"
3737
#include <stdlib.h>
38+
#include <errno.h>
3839
#include <fcntl.h>
3940
#include <unistd.h>
4041

@@ -125,8 +126,10 @@ fmemopen(void *buf, size_t size, const char *mode)
125126

126127
stdio_flags = __stdio_sflags(mode);
127128

128-
if (stdio_flags == 0)
129+
if (stdio_flags == 0 || size == 0) {
130+
errno = EINVAL;
129131
return NULL;
132+
}
130133

131134
/* Allocate file structure and necessary buffers */
132135
mf = calloc(1, sizeof(struct __file_mem));
@@ -135,9 +138,17 @@ fmemopen(void *buf, size_t size, const char *mode)
135138
return NULL;
136139

137140
if (buf == NULL) {
141+
/* POSIX says return EINVAL if: The buf argument is a null pointer and
142+
* the mode argument does not include a '+' character. */
143+
if ((stdio_flags & (__SRD | __SWR)) != (__SRD | __SWR)) {
144+
free(mf);
145+
errno = EINVAL;
146+
return NULL;
147+
}
138148
buf = malloc(size);
139149
if (!buf) {
140150
free(mf);
151+
errno = ENOMEM;
141152
return NULL;
142153
}
143154
mflags |= __MALL;

0 commit comments

Comments
 (0)