From 2eb8cf961ee12bad5b8e93333991f72c71dc4a29 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Wed, 30 Jan 2019 00:12:12 +0000 Subject: [PATCH] Use strncpy for string_utils functions Also removed mstrjoin3 since it's not used. Signed-off-by: Yuxuan Shui --- src/opengl.c | 2 +- src/string_utils.c | 39 +++++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/opengl.c b/src/opengl.c index eafd5223..84744782 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -561,7 +561,7 @@ glx_init_blur(session_t *ps) { const char *texture_func = (use_texture_rect ? "texture2DRect": "texture2D"); const char *shader_add = FRAG_SHADER_BLUR_ADD; - char *extension = strdup(""); + char *extension = NULL; if (use_texture_rect) mstrextend(&extension, "#extension GL_ARB_texture_rectangle : require\n"); if (ps->o.glx_use_gpushader4) { diff --git a/src/string_utils.c b/src/string_utils.c index 064db9c4..df0ac8dd 100644 --- a/src/string_utils.c +++ b/src/string_utils.c @@ -11,33 +11,32 @@ * Allocate the space and join two strings. */ char *mstrjoin(const char *src1, const char *src2) { - auto str = ccalloc(strlen(src1)+strlen(src2)+1, char); + auto len1 = strlen(src1); + auto len2 = strlen(src2); + auto len = len1 + len2 + 1; + auto str = ccalloc(len, char); - strcpy(str, src1); - strcat(str, src2); + strncpy(str, src1, len1); + strncpy(str + len1, src2, len2); + str[len - 1] = '\0'; - return str; -} - -/** - * Allocate the space and join two strings; - */ -char * -mstrjoin3(const char *src1, const char *src2, const char *src3) { - auto str = ccalloc(strlen(src1)+strlen(src2)+strlen(src3)+1, char); - - strcpy(str, src1); - strcat(str, src2); - strcat(str, src3); - - return str; + return str; } /** * Concatenate a string on heap with another string. */ void mstrextend(char **psrc1, const char *src2) { - *psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1) : 0)+strlen(src2)+1); + if (!*psrc1) { + *psrc1 = strdup(src2); + return; + } - strcat(*psrc1, src2); + auto len1 = strlen(*psrc1); + auto len2 = strlen(src2); + auto len = len1 + len2 + 1; + *psrc1 = crealloc(*psrc1, len); + + strncpy(*psrc1 + len1, src2, len2); + (*psrc1)[len - 1] = '\0'; }