Skip to content

Commit fc99cf9

Browse files
committed
__swsetup: set error flag and errno on error.
Previously, we set errno to EBADF if the cantwrite() macro (which calls __swsetup()) returns true for POSIX compliance. However, we neglected to also set the error flag, __SERR. Rather than set the error flag in all callers of cantwrite(), set both errno and the error flag in __swsetup(). This matches what FreeBSD does and makes it possible to choose a proper errno value for the second error condition in __swsetup(). OK deraadt@
1 parent fd2521f commit fc99cf9

File tree

6 files changed

+21
-27
lines changed

6 files changed

+21
-27
lines changed

lib/libc/stdio/fvwrite.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: fvwrite.c,v 1.20 2017/03/17 16:06:33 millert Exp $ */
1+
/* $OpenBSD: fvwrite.c,v 1.21 2023/10/06 16:41:02 millert Exp $ */
22
/*-
33
* Copyright (c) 1990, 1993
44
* The Regents of the University of California. All rights reserved.
@@ -34,7 +34,6 @@
3434
#include <stdio.h>
3535
#include <stdlib.h>
3636
#include <string.h>
37-
#include <errno.h>
3837
#include <unistd.h>
3938
#include "local.h"
4039
#include "fvwrite.h"
@@ -58,10 +57,8 @@ __sfvwrite(FILE *fp, struct __suio *uio)
5857
if ((len = uio->uio_resid) == 0)
5958
return (0);
6059
/* make sure we can write */
61-
if (cantwrite(fp)) {
62-
errno = EBADF;
60+
if (cantwrite(fp))
6361
return (EOF);
64-
}
6562

6663
#define MIN(a, b) ((a) < (b) ? (a) : (b))
6764
#define COPY(n) (void)memcpy(fp->_p, p, n)

lib/libc/stdio/putc.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: putc.c,v 1.13 2015/08/31 02:53:57 guenther Exp $ */
1+
/* $OpenBSD: putc.c,v 1.14 2023/10/06 16:41:02 millert Exp $ */
22
/*-
33
* Copyright (c) 1990, 1993
44
* The Regents of the University of California. All rights reserved.
@@ -32,7 +32,6 @@
3232
*/
3333

3434
#include <stdio.h>
35-
#include <errno.h>
3635
#include "local.h"
3736

3837
/*
@@ -43,10 +42,8 @@
4342
int
4443
putc_unlocked(int c, FILE *fp)
4544
{
46-
if (cantwrite(fp)) {
47-
errno = EBADF;
45+
if (cantwrite(fp))
4846
return (EOF);
49-
}
5047
_SET_ORIENTATION(fp, -1);
5148
return (__sputc(c, fp));
5249
}

lib/libc/stdio/vfprintf.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: vfprintf.c,v 1.81 2021/09/08 15:57:27 jca Exp $ */
1+
/* $OpenBSD: vfprintf.c,v 1.82 2023/10/06 16:41:02 millert Exp $ */
22
/*-
33
* Copyright (c) 1990 The Regents of the University of California.
44
* All rights reserved.
@@ -457,10 +457,8 @@ __vfprintf(FILE *fp, const char *fmt0, __va_list ap)
457457

458458
_SET_ORIENTATION(fp, -1);
459459
/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
460-
if (cantwrite(fp)) {
461-
errno = EBADF;
460+
if (cantwrite(fp))
462461
return (EOF);
463-
}
464462

465463
/* optimise fprintf(stderr) (and other unbuffered Unix files) */
466464
if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&

lib/libc/stdio/vfwprintf.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: vfwprintf.c,v 1.22 2021/09/08 15:57:27 jca Exp $ */
1+
/* $OpenBSD: vfwprintf.c,v 1.23 2023/10/06 16:41:02 millert Exp $ */
22
/*-
33
* Copyright (c) 1990 The Regents of the University of California.
44
* All rights reserved.
@@ -451,10 +451,8 @@ __vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, __va_list ap)
451451

452452
_SET_ORIENTATION(fp, 1);
453453
/* sorry, fwprintf(read_only_file, "") returns EOF, not 0 */
454-
if (cantwrite(fp)) {
455-
errno = EBADF;
454+
if (cantwrite(fp))
456455
return (EOF);
457-
}
458456

459457
/* optimise fwprintf(stderr) (and other unbuffered Unix files) */
460458
if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&

lib/libc/stdio/wbuf.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: wbuf.c,v 1.13 2015/08/31 02:53:57 guenther Exp $ */
1+
/* $OpenBSD: wbuf.c,v 1.14 2023/10/06 16:41:02 millert Exp $ */
22
/*-
33
* Copyright (c) 1990, 1993
44
* The Regents of the University of California. All rights reserved.
@@ -32,7 +32,6 @@
3232
*/
3333

3434
#include <stdio.h>
35-
#include <errno.h>
3635
#include "local.h"
3736

3837
/*
@@ -54,10 +53,8 @@ __swbuf(int c, FILE *fp)
5453
* calls might wrap _w from negative to positive.
5554
*/
5655
fp->_w = fp->_lbfsize;
57-
if (cantwrite(fp)) {
58-
errno = EBADF;
56+
if (cantwrite(fp))
5957
return (EOF);
60-
}
6158
c = (unsigned char)c;
6259

6360
/*

lib/libc/stdio/wsetup.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: wsetup.c,v 1.7 2005/08/08 08:05:36 espie Exp $ */
1+
/* $OpenBSD: wsetup.c,v 1.8 2023/10/06 16:41:02 millert Exp $ */
22
/*-
33
* Copyright (c) 1990, 1993
44
* The Regents of the University of California. All rights reserved.
@@ -31,14 +31,15 @@
3131
* SUCH DAMAGE.
3232
*/
3333

34+
#include <errno.h>
3435
#include <stdio.h>
3536
#include <stdlib.h>
3637
#include "local.h"
3738

3839
/*
3940
* Various output routines call wsetup to be sure it is safe to write,
4041
* because either _flags does not include __SWR, or _buf is NULL.
41-
* _wsetup returns 0 if OK to write, nonzero otherwise.
42+
* __swsetup returns 0 if OK to write, nonzero otherwise, setting errno.
4243
*/
4344
int
4445
__swsetup(FILE *fp)
@@ -51,8 +52,11 @@ __swsetup(FILE *fp)
5152
* If we are not writing, we had better be reading and writing.
5253
*/
5354
if ((fp->_flags & __SWR) == 0) {
54-
if ((fp->_flags & __SRW) == 0)
55+
if ((fp->_flags & __SRW) == 0) {
56+
errno = EBADF;
57+
fp->_flags |= __SERR;
5558
return (EOF);
59+
}
5660
if (fp->_flags & __SRD) {
5761
/* clobber any ungetc data */
5862
if (HASUB(fp))
@@ -68,8 +72,11 @@ __swsetup(FILE *fp)
6872
* Make a buffer if necessary, then set _w.
6973
*/
7074
if (fp->_bf._base == NULL) {
71-
if ((fp->_flags & (__SSTR | __SALC)) == __SSTR)
75+
if ((fp->_flags & (__SSTR | __SALC)) == __SSTR) {
76+
errno = EINVAL;
77+
fp->_flags |= __SERR;
7278
return (EOF);
79+
}
7380
__smakebuf(fp);
7481
}
7582
if (fp->_flags & __SLBF) {

0 commit comments

Comments
 (0)