Merge pull request #4831 from fahhem/less_scanf

Remove excessive use of printf/scanf in mbed_fdopen/_open
pull/4905/merge
Jimmy Brisson 2017-08-24 10:12:10 -05:00 committed by GitHub
commit dd0a0fc3e7
1 changed files with 8 additions and 4 deletions

View File

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