Skip to content

Commit e5ef69a

Browse files
committed
Remove excessive use of printf/scanf in mbed_fdopen/_open
1 parent 2305a8c commit e5ef69a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

platform/mbed_retarget.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
231231
/* FILENAME: ":0x12345678" describes a FileHandle* */
232232
if (name[0] == ':') {
233233
void *p;
234-
std::sscanf(name, ":%p", &p);
234+
memcpy(&p, name + 1, sizeof(p));
235235
res = (FileHandle*)p;
236236

237237
/* FILENAME: "/file_system/file_name" */
@@ -826,8 +826,12 @@ void mbed_set_unbuffered_stream(std::FILE *_file) {
826826
*/
827827
std::FILE *mbed_fdopen(FileHandle *fh, const char *mode)
828828
{
829-
char buf[12]; /* :0x12345678 + null byte */
830-
std::sprintf(buf, ":%p", fh);
829+
char buf[2 + sizeof(fh) + 1]; /* :(pointer) + null byte */
830+
static_assert(sizeof(buf) == 7, "Pointers should be 4 bytes.");
831+
buf[0] = ':';
832+
memcpy(buf + 1, &fh, sizeof(fh));
833+
buf[1 + sizeof(fh)] = '\0';
834+
831835
std::FILE *stream = std::fopen(buf, mode);
832836
/* newlib-nano doesn't appear to ever call _isatty itself, so
833837
* happily fully buffers an interactive stream. Deal with that here.

0 commit comments

Comments
 (0)