@@ -228,10 +228,10 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
228
228
229
229
FileHandle *res = NULL ;
230
230
231
- /* FILENAME: ":0x12345678 " describes a FileHandle* */
231
+ /* FILENAME: ":(pointer) " describes a FileHandle* */
232
232
if (name[0 ] == ' :' ) {
233
233
void *p;
234
- std::sscanf (name, " :%p " , &p );
234
+ memcpy (&p, name + 1 , sizeof (p) );
235
235
res = (FileHandle*)p;
236
236
237
237
/* FILENAME: "/file_system/file_name" */
@@ -826,8 +826,12 @@ void mbed_set_unbuffered_stream(std::FILE *_file) {
826
826
*/
827
827
std::FILE *mbed_fdopen (FileHandle *fh, const char *mode)
828
828
{
829
- char buf[12 ]; /* :0x12345678 + null byte */
830
- std::sprintf (buf, " :%p" , fh);
829
+ // This is to avoid scanf(buf, ":%.4s", fh) and the bloat it brings.
830
+ char buf[1 + sizeof (fh)]; /* :(pointer) */
831
+ MBED_STATIC_ASSERT (sizeof (buf) == 5 , " Pointers should be 4 bytes." );
832
+ buf[0 ] = ' :' ;
833
+ memcpy (buf + 1 , &fh, sizeof (fh));
834
+
831
835
std::FILE *stream = std::fopen (buf, mode);
832
836
/* newlib-nano doesn't appear to ever call _isatty itself, so
833
837
* happily fully buffers an interactive stream. Deal with that here.
0 commit comments