From d29196d9e2a00cd556f9452259147e02692b6bb2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 22 Jul 2020 07:17:44 +0200 Subject: Get rid of str[n]cpy() calls --- genx.c | 15 ++++++++++----- makefile | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/genx.c b/genx.c index 685c54a..b2b999f 100644 --- a/genx.c +++ b/genx.c @@ -179,10 +179,13 @@ static void deallocate(genxWriter w, void * data) static utf8 copy(genxWriter w, constUtf8 from) { utf8 temp; + size_t sl = strlen((const char *) from); - if ((temp = (utf8) allocate(w, strlen((const char *) from) + 1)) == NULL) + if ((temp = (utf8) allocate(w, sl + 1)) == NULL) return NULL; - strcpy((char *) temp, (const char *) from); + + memcpy(temp, from, sl); + temp[sl] = 0; return temp; } @@ -203,7 +206,7 @@ static genxStatus growCollector(genxWriter w, collector * c, size_t size) if ((newSpace = (utf8) allocate(w, c->space)) == NULL) return GENX_ALLOC_FAILED; - strncpy((char *) newSpace, (const char *) c->buf, c->used); + memcpy(newSpace, c->buf, c->used); newSpace[c->used] = 0; deallocate(w, c->buf); c->buf = newSpace; @@ -227,11 +230,13 @@ static genxStatus collectString(genxWriter w, collector * c, constUtf8 string) if ((w->status = growCollector(w, c, sl)) != GENX_SUCCESS) return GENX_ALLOC_FAILED; - strcpy((char *) c->buf, (const char *) string); + memcpy(c->buf, string, sl); + c->buf[sl] = 0; return GENX_SUCCESS; } -#define collectPiece(w,c,d,size) {if (((c)->used+(size))>=(c)->space){if (((w)->status=growCollector(w,c,(c)->used+(size)))!=GENX_SUCCESS) return (w)->status;}strncpy((char *)(c)->buf+(c)->used,d,size);(c)->used+=size;} +/* Note: does not add the trailing '\0' (done by endCollect() call). */ +#define collectPiece(w,c,d,size) {if (((c)->used+(size))>=(c)->space){if (((w)->status=growCollector(w,c,(c)->used+(size)))!=GENX_SUCCESS) return (w)->status;}memcpy((char *)(c)->buf+(c)->used,d,size);(c)->used+=size;} /******************************* * private list utilities diff --git a/makefile b/makefile index ab06a68..0625c7a 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ src := genx.c char-props.c CC := gcc -CFLAGS := -W -Wall +CFLAGS := -Wall -Wextra -Werror # Compile. # -- cgit v1.1