Skip to content

Commit a7f167a

Browse files
committed
OSReport: fix sending message when its larger than the buffer
1 parent 52c525a commit a7f167a

1 file changed

Lines changed: 49 additions & 25 deletions

File tree

libogc/sys_report.c

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ distribution.
3030
#include <sys/iosupport.h>
3131
#include <stdio.h>
3232
#include <stdarg.h>
33+
#include <malloc.h>
3334

3435
#include "exi.h"
3536

@@ -57,28 +58,32 @@ static ssize_t __uart_write(const char *buffer,size_t len)
5758

5859
#define __outsz 256
5960

60-
61-
static char __outstr[__outsz];
62-
6361
static ssize_t __uart_stdio_write(struct _reent *r, void *fd, const char *ptr, size_t len)
6462
{
6563
// translate \n and \r\n to \r for Dolphin handling
66-
const char *p = ptr;
67-
size_t left = len;
68-
while(left>0) {
69-
char *buf = __outstr;
70-
size_t buflen = len;
71-
if (buflen > __outsz) buflen = __outsz;
72-
left -= buflen;
73-
while(buflen>0) {
74-
char ch = *p++;
75-
if(ch=='\r' && *p == '\n') continue;
76-
if (ch=='\n') ch = '\r';
77-
*buf++ = ch;
78-
buflen--;
64+
char *p = (char*)ptr;
65+
char *buf = (char*)ptr;
66+
size_t send_len = len;
67+
68+
for(size_t i = 0; i <= send_len; i++)
69+
{
70+
char ch = *p++;
71+
//skip the one of the 2 win newline bytes
72+
if(ch=='\r' && *p == '\n')
73+
{
74+
send_len--;
75+
continue;
7976
}
80-
__uart_write(__outstr,len);
77+
78+
//newline should be converted to carriage return
79+
if (ch=='\n')
80+
ch = '\r';
81+
82+
*buf++ = ch;
8183
}
84+
85+
__uart_write(ptr, send_len);
86+
8287
return len;
8388
}
8489
static const devoptab_t dotab_uart = {
@@ -99,19 +104,38 @@ void SYS_STDIO_Report(bool use_stdout)
99104
}
100105
}
101106

102-
103107
void SYS_Report (char const *const fmt_, ...)
104108
{
105-
106-
size_t len;
107-
109+
ssize_t len = 0;
110+
char* buffer = NULL;
108111
va_list args;
112+
size_t write_len = __outsz;
113+
while(true)
114+
{
115+
char* tmp = (char*)realloc(buffer, write_len);
116+
if(tmp == NULL)
117+
break;
118+
119+
buffer = tmp;
120+
121+
va_start(args, fmt_);
122+
len = vsnprintf(buffer, write_len, fmt_, args);
123+
va_end(args);
124+
if(len >= write_len)
125+
{
126+
write_len = len+1;
127+
continue;
128+
}
109129

110-
va_start(args, fmt_);
111-
len=vsnprintf(__outstr,256,fmt_,args);
112-
va_end(args);
130+
memset(buffer + len, 0, write_len - len);
131+
break;
132+
}
113133

114-
__uart_stdio_write(NULL, 0, __outstr, len);
134+
//an error must have occured
135+
if(buffer != NULL && len > 0)
136+
__uart_stdio_write(NULL, 0, buffer, len);
115137

138+
if(buffer)
139+
free(buffer);
116140
}
117141

0 commit comments

Comments
 (0)