From: Dale Weiler Date: Sun, 14 Apr 2013 01:00:25 +0000 (+0000) Subject: flatten the use of strcpy, 90% of the cases we already knew the length of the string... X-Git-Tag: before-library~73 X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=commitdiff_plain;h=48d6375817d3834e934bec6849cdd08daaa3c9b2 flatten the use of strcpy, 90% of the cases we already knew the length of the string either at compile-time, or already within the scope we where, thus letting us use strncpy, which can be further optimized (unrolled if static) --- diff --git a/Makefile b/Makefile index c72b237..b3d1bc3 100644 --- a/Makefile +++ b/Makefile @@ -160,7 +160,6 @@ SPLINTFLAGS = \ -realcompare \ -observertrans \ -shiftnegative \ - -freshtrans \ -abstract \ -statictrans \ -castfcnptr diff --git a/ast.c b/ast.c index 13adffa..38c95a4 100644 --- a/ast.c +++ b/ast.c @@ -218,7 +218,7 @@ static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsi if (!e) { if (pos + 6 >= bufsize) goto full; - strcpy(buf + pos, "(null)"); + strncpy(buf + pos, "(null)", 6); return pos + 6; } @@ -227,7 +227,7 @@ static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsi switch (e->expression.vtype) { case TYPE_VARIANT: - strcpy(buf + pos, "(variant)"); + strncpy(buf + pos, "(variant)", 9); return pos + 9; case TYPE_FIELD: @@ -284,7 +284,7 @@ static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsi typelen = strlen(typestr); if (pos + typelen >= bufsize) goto full; - strcpy(buf + pos, typestr); + strncpy(buf + pos, typestr, typelen); return pos + typelen; } @@ -1216,7 +1216,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) namelen = strlen(self->name); name = (char*)mem_a(namelen + 16); - strcpy(name, self->name); + strncpy(name, self->name, namelen); array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count); array->ir_values[0] = v; @@ -1274,7 +1274,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield) namelen = strlen(self->name); name = (char*)mem_a(namelen + 16); - strcpy(name, self->name); + strncpy(name, self->name, namelen); self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count); self->ir_values[0] = v; @@ -1416,7 +1416,7 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param) namelen = strlen(self->name); name = (char*)mem_a(namelen + 16); - strcpy(name, self->name); + strncpy(name, self->name, namelen); self->ir_values[0] = v; for (ai = 1; ai < self->expression.count; ++ai) { diff --git a/code.c b/code.c index 5728a8d..1026693 100644 --- a/code.c +++ b/code.c @@ -36,7 +36,7 @@ uint32_t code_entfields; /* This is outrageous! */ #define QCINT_ENTRY void* #define QCINT_TO_HASH_ENTRY(q) ((void*)(uintptr_t)(q)) -#define HASH_ENTRY_TO_QCINT(h) ((qcint)(uintptr_t)(h)) +#define HASH_ENTRY_TO_QCINT(h) /*@only@*/ ((qcint) *((uintptr_t*)(&(h))) ) static ht code_string_cache; static qcint code_string_cached_empty; diff --git a/fs.c b/fs.c index 8d73b1d..1461bf9 100644 --- a/fs.c +++ b/fs.c @@ -238,7 +238,7 @@ int fs_file_getline(char **lineptr, size_t *n, FILE *stream) { if (!dir) return NULL; - strcpy(dir->dd_name, name); + strncpy(dir->dd_name, name, strlen(name)); return dir; } @@ -258,8 +258,8 @@ int fs_file_getline(char **lineptr, size_t *n, FILE *stream) { if (*dir->dd_name) { size_t n = strlen(dir->dd_name); if ((dirname = (char*)mem_a(n + 5) /* 4 + 1 */)) { - strcpy(dirname, dir->dd_name); - strcpy(dirname + n, "\\*.*"); /* 4 + 1 */ + strncpy(dirname, dir->dd_name, n); + strncpy(dirname + n, "\\*.*", 4); /* 4 + 1 */ } } else { if (!(dirname = util_strdup("\\*.*"))) diff --git a/pak.c b/pak.c index 043ef89..fd80dea 100644 --- a/pak.c +++ b/pak.c @@ -361,7 +361,7 @@ bool pak_insert_one(pak_file_t *pak, const char *file) { return false; } - strcpy(dir.name, file); + strncpy(dir.name, file, strlen(file)); /* * Allocate some memory for loading in the data that will be