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)

This commit is contained in:
Dale Weiler 2013-04-14 01:00:25 +00:00
parent 3f40fcb965
commit 48d6375817
5 changed files with 11 additions and 12 deletions

View file

@ -160,7 +160,6 @@ SPLINTFLAGS = \
-realcompare \
-observertrans \
-shiftnegative \
-freshtrans \
-abstract \
-statictrans \
-castfcnptr

12
ast.c
View file

@ -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) {

2
code.c
View file

@ -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;

6
fs.c
View file

@ -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("\\*.*")))

2
pak.c
View file

@ -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