shithub: riscv

Download patch

ref: 1987cc69c8c2f071762da025a84903dcc8197855
parent: ad26dc48a6dc05da02f96e5f1e7b82de58ff5b2c
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jul 11 09:28:58 EDT 2020

stdio, ape/stdio: fix order of operations in putc

When calling putc, we need to return either EOF
or the character returned. To distinguish the
two, we need to avoid sign extending 0xff. The
code attempted to do this, but the order of
operations was wrong, so we ended up masking,
setting a character, and then sign extending
the character.

This fixes things so we mask after assignment.

--- a/sys/include/ape/stdio.h
+++ b/sys/include/ape/stdio.h
@@ -106,7 +106,7 @@
 #define	getchar()	getc(stdin)
 extern char *gets(char *);
 extern int putc(int, FILE *);
-#define	putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):(*(f)->wp++=(c)&_IO_CHMASK))
+#define	putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):((*(f)->wp++=(c))&_IO_CHMASK))
 extern int _IO_putc(int, FILE *);
 extern int putchar(int);
 #define	putchar(c)	putc(c, stdout)
--- a/sys/include/stdio.h
+++ b/sys/include/stdio.h
@@ -92,7 +92,7 @@
 #define	getchar()	getc(stdin)
 char *gets(char *);
 int putc(int, FILE *);
-#define	putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):(*(f)->wp++=(c)&_IO_CHMASK))
+#define	putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):((*(f)->wp++=(c))&_IO_CHMASK))
 int _IO_putc(int, FILE *);
 int putchar(int);
 #define	putchar(c)	putc(c, stdout)