aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavide Grohmann <davide.grohmann@arm.com>2021-08-13 13:01:00 +0200
committerDavide Grohmann <davide.grohmann@arm.com>2021-08-18 15:51:14 +0200
commitb62e944b466e41dce4495162210ef3af59dfcb29 (patch)
treef41d54a92d06243e28f57ae295a7f205e69e6a2c
parent03e19e2ab37bf1d34ac03e5a71c3b243898d2223 (diff)
downloadethos-u-core-platform-b62e944b466e41dce4495162210ef3af59dfcb29.tar.gz
Fix: return value for GCC _write and _read is written/read chars21.08-rc321.08-rc221.08
When using ARMClang compiler the functions _sys_write and _sys_read should return the number of unprocessed characters, whilst when using GCC the functions _write and _read should return the number of processed (i.e., written or read) chars. The code was returning the unprocessed characters value in both cases (i.e., 0) making the calling code to exit early with error and stop further processing when using GCC. Hence no subsequent calls to write would be executed causing missing output on the console. For example, string truncation has been seen when printing on stderr. Note that the bug was not really visible when printing to stdout, but it was immediately exposed when using stderr. This was likely due to the fact that libc buffers the stdout output and flushes it only when encountering a new line char (\n), whilst no buffering is done on stderr. Indeed by printing whole buffers the problem was worked around and went unseen. Change-Id: I5a48d6a29441175b2a950716997332a8b9c34e10
-rw-r--r--targets/corstone-300/retarget.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/targets/corstone-300/retarget.c b/targets/corstone-300/retarget.c
index 13cf68b..4bde44d 100644
--- a/targets/corstone-300/retarget.c
+++ b/targets/corstone-300/retarget.c
@@ -33,7 +33,8 @@
#define STDOUT 0x8002
#define STDERR 0x8003
-#define RETARGET(fun) _sys##fun
+#define RETARGET(fun) _sys##fun
+#define IO_OUTPUT(len) 0
#else
/*
@@ -52,7 +53,8 @@ extern FILEHANDLE _open(const char * /*name*/, int /*openmode*/);
#define STDOUT 0x01
#define STDERR 0x02
-#define RETARGET(fun) fun
+#define RETARGET(fun) fun
+#define IO_OUTPUT(len) len
#endif
@@ -90,15 +92,16 @@ int RETARGET(_write)(FILEHANDLE fh, const unsigned char *buf, unsigned int len,
case STDOUT:
case STDERR: {
int c;
+ unsigned int i;
- while (len-- > 0) {
- c = fputc(*buf++, stdout);
+ for (i = 0; i < len; i++) {
+ c = fputc(buf[i], stdout);
if (c == EOF) {
return EOF;
}
}
- return 0;
+ return IO_OUTPUT(len);
}
default:
return EOF;
@@ -111,17 +114,18 @@ int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mod
switch (fh) {
case STDIN: {
int c;
+ unsigned int i;
- while (len-- > 0) {
+ for (i = 0; i < len; i++) {
c = fgetc(stdin);
if (c == EOF) {
return EOF;
}
- *buf++ = (unsigned char)c;
+ buf[i] = (unsigned char)c;
}
- return 0;
+ return IO_OUTPUT(len);
}
default:
return EOF;