diff --git a/scp.c b/scp.c index e72ce8dd..7a509aa1 100644 --- a/scp.c +++ b/scp.c @@ -2929,23 +2929,19 @@ free (*tmpname); static t_stat sim_snprint_sym (char *buf, size_t bufsize, t_bool vm_flag, t_addr addr, t_value *val, UNIT *uptr, int32 sw, int32 dfltinc, int32 rdx, uint32 width, uint32 fmt) { t_stat reason; -size_t str_width; -size_t s; +MEMFILE mbuf; -rewind (sim_tmpfile); +memset (&mbuf, 0, sizeof (mbuf)); +mbuf.buf = malloc (512); /* Pre allocate a memory buffer to avoid */ +mbuf.size = 512; /* potential double vsnprintf to expand the buffer */ +sim_mfile = &mbuf; if (vm_flag || ((reason = fprint_sym (sim_tmpfile, addr, val, uptr, sw)) > 0)) { fprint_val (sim_tmpfile, val[0], rdx, width, fmt); reason = dfltinc; } -str_width = (size_t)ftell(sim_tmpfile); -if (str_width > width) - str_width = width; -if (bufsize > str_width) - memset (buf + str_width, 0, bufsize - width); -rewind (sim_tmpfile); -s = fread (buf, 1, str_width, sim_tmpfile); -if (s < bufsize) - buf[s] = '\0'; +sim_mfile = NULL; +strlcpy (buf, mbuf.buf, MIN(bufsize, mbuf.pos + 1)); +free (mbuf.buf); return reason; }