From 1048ed82f22fe57f1e139821ae3a3ce6a52f1002 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Wed, 7 Feb 2018 12:46:46 -0800 Subject: [PATCH] Purge `strncpy` from the tests and docs. NFC. The existing uses of `strncpy` rightly triggered some static analysis warnings from Coverity, because they were consistently being used in the pattern strncpy(destbuf, src, sizeof destbuf); TREAT_AS_STRING(destbuf) which is wrong: `strncpy` is what you use specifically when there is a chance that the destination buffer will *not* wind up null-terminated. In cases like these, we expect (and know) that the buffer is big enough to store the entire string plus a null terminator; and so `strcpy` is the correct tool for this job. (Or arguably `snprintf`, but that's C99 and we still compile these tests in C89 mode.) Coverity continues to complain about the `strcpy` itself, in these tests: `test12.c, test15.c, test66.c, test83.c`. --- doc/userguide.txt | 36 +++++++++++++++++------------------- doc/utlist.txt | 4 ++-- tests/bloom_perf.c | 2 +- tests/emit_keys.c | 2 +- tests/test11.c | 2 +- tests/test12.c | 2 +- tests/test14.c | 2 +- tests/test15.c | 2 +- tests/test26.c | 2 +- tests/test29.c | 2 +- tests/test30.c | 2 +- tests/test31.c | 2 +- tests/test32.c | 2 +- tests/test33.c | 2 +- tests/test34.c | 2 +- tests/test35.c | 5 ++--- tests/test56.c | 2 +- tests/test66.c | 2 +- tests/test83.c | 2 +- tests/test84.c | 4 ++-- 20 files changed, 39 insertions(+), 42 deletions(-) diff --git a/doc/userguide.txt b/doc/userguide.txt index c569d07c..c2d7af13 100644 --- a/doc/userguide.txt +++ b/doc/userguide.txt @@ -257,7 +257,7 @@ Otherwise we just modify the structure that already exists. HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */ if (s==NULL) { - s = (struct my_struct*)malloc(sizeof(struct my_struct)); + s = (struct my_struct *)malloc(sizeof *s); s->id = user_id; HASH_ADD_INT( users, id, s ); /* id: name of key field */ } @@ -534,7 +534,7 @@ void add_user(int user_id, char *name) { HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */ if (s==NULL) { - s = (struct my_struct*)malloc(sizeof(struct my_struct)); + s = (struct my_struct *)malloc(sizeof *s); s->id = user_id; HASH_ADD_INT( users, id, s ); /* id: name of key field */ } @@ -713,14 +713,13 @@ struct my_struct { int main(int argc, char *argv[]) { - const char **n, *names[] = { "joe", "bob", "betty", NULL }; + const char *names[] = { "joe", "bob", "betty", NULL }; struct my_struct *s, *tmp, *users = NULL; - int i=0; - for (n = names; *n != NULL; n++) { - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - strncpy(s->name, *n,10); - s->id = i++; + for (int i = 0; names[i]; ++i) { + s = (struct my_struct *)malloc(sizeof *s); + strcpy(s->name, names[i]); + s->id = i; HASH_ADD_STR( users, name, s ); } @@ -760,14 +759,13 @@ struct my_struct { int main(int argc, char *argv[]) { - const char **n, *names[] = { "joe", "bob", "betty", NULL }; + const char *names[] = { "joe", "bob", "betty", NULL }; struct my_struct *s, *tmp, *users = NULL; - int i=0; - for (n = names; *n != NULL; n++) { - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - s->name = *n; - s->id = i++; + for (int i = 0; names[i]; ++i) { + s = (struct my_struct *)malloc(sizeof *s); + s->name = names[i]; + s->id = i; HASH_ADD_KEYPTR( hh, users, s->name, strlen(s->name), s ); } @@ -810,7 +808,7 @@ char *someaddr = NULL; int main() { el_t *d; - el_t *e = (el_t*)malloc(sizeof(el_t)); + el_t *e = (el_t *)malloc(sizeof *e); if (!e) return -1; e->key = (void*)someaddr; e->i = 1; @@ -861,8 +859,8 @@ typedef struct { int main(int argc, char *argv[]) { record_t l, *p, *r, *tmp, *records = NULL; - r = (record_t*)malloc( sizeof(record_t) ); - memset(r, 0, sizeof(record_t)); + r = (record_t *)malloc(sizeof *r); + memset(r, 0, sizeof *r); r->key.a = 'a'; r->key.b = 1; HASH_ADD(hh, records, key, sizeof(record_key_t), r); @@ -926,7 +924,7 @@ int main(int argc, char *argv[]) { int beijing[] = {0x5317, 0x4eac}; /* UTF-32LE for 北京 */ /* allocate and initialize our structure */ - msg = (msg_t*)malloc( sizeof(msg_t) + sizeof(beijing) ); + msg = (msg_t *)malloc( sizeof(msg_t) + sizeof(beijing) ); memset(msg, 0, sizeof(msg_t)+sizeof(beijing)); /* zero fill */ msg->len = sizeof(beijing); msg->encoding = UTF32; @@ -943,7 +941,7 @@ int main(int argc, char *argv[]) { /* look it up to prove that it worked :-) */ msg=NULL; - lookup_key = (lookup_key_t*)malloc(sizeof(*lookup_key) + sizeof(beijing)); + lookup_key = (lookup_key_t *)malloc(sizeof(*lookup_key) + sizeof(beijing)); memset(lookup_key, 0, sizeof(*lookup_key) + sizeof(beijing)); lookup_key->encoding = UTF32; memcpy(lookup_key->text, beijing, sizeof(beijing)); diff --git a/doc/utlist.txt b/doc/utlist.txt index 72765c47..56920ded 100644 --- a/doc/utlist.txt +++ b/doc/utlist.txt @@ -234,8 +234,8 @@ int main(int argc, char *argv[]) { } while (fgets(linebuf,BUFLEN,file) != NULL) { - if ( (name = (el*)malloc(sizeof(el))) == NULL) exit(-1); - strncpy(name->bname,linebuf,BUFLEN); + if ( (name = (el *)malloc(sizeof *name)) == NULL) exit(-1); + strcpy(name->bname, linebuf); DL_APPEND(head, name); } DL_SORT(head, namecmp); diff --git a/tests/bloom_perf.c b/tests/bloom_perf.c index 9d667c8a..6a9abf19 100644 --- a/tests/bloom_perf.c +++ b/tests/bloom_perf.c @@ -37,7 +37,7 @@ int main(int argc,char *argv[]) if ( (name = (name_rec*)malloc(sizeof(name_rec))) == NULL) { exit(-1); } - strncpy(name->boy_name,linebuf,BUFLEN); + strcpy(name->boy_name, linebuf); HASH_ADD_STR(names,boy_name,name); } diff --git a/tests/emit_keys.c b/tests/emit_keys.c index 34054899..0b98689e 100644 --- a/tests/emit_keys.c +++ b/tests/emit_keys.c @@ -36,7 +36,7 @@ int main(int argc,char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->boy_name,linebuf,sizeof(name->boy_name)); + strcpy(name->boy_name, linebuf); HASH_ADD_STR(names,boy_name,name); i++; } diff --git a/tests/test11.c b/tests/test11.c index 08331b47..8fe72cda 100644 --- a/tests/test11.c +++ b/tests/test11.c @@ -42,7 +42,7 @@ int main(int argc,char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->boy_name,linebuf,sizeof(name->boy_name)); + strcpy(name->boy_name, linebuf); HASH_ADD_STR(names,boy_name,name); } diff --git a/tests/test12.c b/tests/test12.c index f609c34e..561c52ba 100644 --- a/tests/test12.c +++ b/tests/test12.c @@ -22,7 +22,7 @@ int main(int argc, char*argv[]) if (person == NULL) { exit(-1); } - strncpy(person->first_name, *name,10UL); + strcpy(person->first_name, *name); person->id = id++; HASH_ADD_STR(people,first_name,person); printf("added %s (id %d)\n", person->first_name, person->id); diff --git a/tests/test14.c b/tests/test14.c index cf7345e1..d5ced051 100644 --- a/tests/test14.c +++ b/tests/test14.c @@ -33,7 +33,7 @@ int main(int argc,char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->boy_name,linebuf,sizeof(name->boy_name)); + strcpy(name->boy_name, linebuf); HASH_ADD_STR(names,boy_name,name); } diff --git a/tests/test15.c b/tests/test15.c index 0659185d..a8867908 100644 --- a/tests/test15.c +++ b/tests/test15.c @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) if (s == NULL) { exit(-1); } - strncpy(s->name, *n,10UL); + strcpy(s->name, *n); s->id = i++; HASH_ADD_STR( users, name, s ); } diff --git a/tests/test26.c b/tests/test26.c index 7f8a068b..59b80a39 100644 --- a/tests/test26.c +++ b/tests/test26.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); DL_APPEND(head, name); } DL_SORT(head, namecmp); diff --git a/tests/test29.c b/tests/test29.c index ae2fbb84..a699f9a4 100644 --- a/tests/test29.c +++ b/tests/test29.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); DL_APPEND(head, name); } DL_SORT(head, namecmp); diff --git a/tests/test30.c b/tests/test30.c index e3700fc9..0eb7c5d4 100644 --- a/tests/test30.c +++ b/tests/test30.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); CDL_PREPEND(head, name); } CDL_SORT(head, namecmp); diff --git a/tests/test31.c b/tests/test31.c index e3700fc9..0eb7c5d4 100644 --- a/tests/test31.c +++ b/tests/test31.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); CDL_PREPEND(head, name); } CDL_SORT(head, namecmp); diff --git a/tests/test32.c b/tests/test32.c index c09caa36..de53e122 100644 --- a/tests/test32.c +++ b/tests/test32.c @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); DL_PREPEND(head, name); } /* DL_SORT(head, namecmp); */ diff --git a/tests/test33.c b/tests/test33.c index 51aa1487..53980f63 100644 --- a/tests/test33.c +++ b/tests/test33.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); LL_PREPEND(head, name); } LL_SORT(head, namecmp); diff --git a/tests/test34.c b/tests/test34.c index 15c3a6fa..333439a3 100644 --- a/tests/test34.c +++ b/tests/test34.c @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); CDL_PREPEND(head, name); } /* CDL_SORT(head, namecmp); */ diff --git a/tests/test35.c b/tests/test35.c index 4cbb6c0a..729b5c95 100644 --- a/tests/test35.c +++ b/tests/test35.c @@ -13,17 +13,16 @@ int main(int argc,char *argv[]) int i; elt *head = NULL; elt elts[10]; - char label[6]; + char label[6] = "hello"; for(i=0; i<10; i++) { elts[i].s = (char*)malloc(6UL); - strncpy(elts[i].s, "hello",6UL); + strcpy(elts[i].s, "hello"); elts[i].s[0] = 'a' + i; printf("%d: %s\n", i, elts[i].s); HASH_ADD_KEYPTR(hh, head, elts[i].s, 6UL, &elts[i]); } /* look up each element and verify the result pointer */ - strncpy(label, "hello", sizeof(label)); for(i=0; i<10; i++) { elt *e; label[0] = 'a' + i; diff --git a/tests/test56.c b/tests/test56.c index a130ff89..3db4002e 100644 --- a/tests/test56.c +++ b/tests/test56.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) if (name == NULL) { exit(-1); } - strncpy(name->bname,linebuf,sizeof(name->bname)); + strcpy(name->bname, linebuf); DL_APPEND(head, name); } DL_SORT(head, namecmp); diff --git a/tests/test66.c b/tests/test66.c index aa49172f..100c62be 100644 --- a/tests/test66.c +++ b/tests/test66.c @@ -22,7 +22,7 @@ int main(int argc, char*argv[]) if (person == NULL) { exit(-1); } - strncpy(person->first_name, *name,sizeof(person->first_name)); + strcpy(person->first_name, *name); person->id = id++; HASH_ADD_STR(people,first_name,person); printf("added %s (id %d)\n", person->first_name, person->id); diff --git a/tests/test83.c b/tests/test83.c index 494ec7b2..588edb02 100644 --- a/tests/test83.c +++ b/tests/test83.c @@ -22,7 +22,7 @@ int main(int argc, char*argv[]) if (person == NULL) { exit(-1); } - strncpy(person->first_name, *name,sizeof(person->first_name)); + strcpy(person->first_name, *name); person->id = id++; HASH_ADD_STR(people,first_name,person); printf("added %s (id %d)\n", person->first_name, person->id); diff --git a/tests/test84.c b/tests/test84.c index eb9fb04f..99ec7a0d 100644 --- a/tests/test84.c +++ b/tests/test84.c @@ -26,7 +26,7 @@ int main(int argc, char*argv[]) if (person->first_name == NULL) { exit(-1); } - strncpy(person->first_name, *name,10UL); + strcpy(person->first_name, *name); person->id = id++; HASH_ADD_STR(people,first_name,person); printf("added %s (id %d)\n", person->first_name, person->id); @@ -47,7 +47,7 @@ int main(int argc, char*argv[]) if (new_person->first_name == NULL) { exit(-1); } - strncpy(new_person->first_name, person->first_name,10UL); + strcpy(new_person->first_name, person->first_name); new_person->id = person->id*10; HASH_REPLACE_STR(people,first_name,new_person,tmp); printf("replaced (%c) with %s (id %d)\n", (tmp!=NULL)?'y':'n', new_person->first_name, new_person->id);