From 21cc2dc13d48aaa70b88d7a190ea8b4b830986f4 Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Fri, 3 Feb 2017 09:03:27 -0500 Subject: In set_profile_text, use emalloc instead of estrdup to improve clarity. --- ChangeLog | 5 +++++ awkgram.c | 7 ++++--- awkgram.y | 7 ++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6fe653f3..cb9a0115 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2017-02-03 Andrew J. Schorr + + * awkgram.y (set_profile_text): Improve code clarity by using emalloc + to allocate the string instead of abusing estrdup. + 2017-02-02 Arnold D. Robbins * awkgram.y (set_profile_next): Allocate an extra byte at the diff --git a/awkgram.c b/awkgram.c index a39fff22..3c26d34b 100644 --- a/awkgram.c +++ b/awkgram.c @@ -8712,9 +8712,10 @@ static NODE * set_profile_text(NODE *n, const char *str, size_t len) { if (do_pretty_print) { - // extra byte in case we need to add minus sign in negate_num - // note that estrdup adds another byte for the \0 at the end - n->stptr = estrdup(str, len + 1); + // two extra bytes: one for NUL termination, and another in + // case we need to add a leading minus sign in add_sign_to_num + emalloc(n->stptr, char *, len + 2, "set_profile_text"); + memcpy(n->stptr, str, len); n->stptr[len] = '\0'; n->stlen = len; // Set STRCUR and n->stfmt for use when profiling diff --git a/awkgram.y b/awkgram.y index f506be80..ea365a76 100644 --- a/awkgram.y +++ b/awkgram.y @@ -6292,9 +6292,10 @@ static NODE * set_profile_text(NODE *n, const char *str, size_t len) { if (do_pretty_print) { - // extra byte in case we need to add minus sign in negate_num - // note that estrdup adds another byte for the \0 at the end - n->stptr = estrdup(str, len + 1); + // two extra bytes: one for NUL termination, and another in + // case we need to add a leading minus sign in add_sign_to_num + emalloc(n->stptr, char *, len + 2, "set_profile_text"); + memcpy(n->stptr, str, len); n->stptr[len] = '\0'; n->stlen = len; // Set STRCUR and n->stfmt for use when profiling -- cgit v1.2.3