Skip to content

Commit

Permalink
Purge strncpy from the tests and docs. NFC.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Quuxplusone committed Feb 7, 2018
1 parent 29dbbd3 commit 1048ed8
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 42 deletions.
36 changes: 17 additions & 19 deletions doc/userguide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
Expand Down Expand Up @@ -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 */
}
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions doc/utlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/bloom_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/emit_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test12.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test14.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test15.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test26.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test29.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test30.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test31.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); */
Expand Down
2 changes: 1 addition & 1 deletion tests/test33.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test34.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); */
Expand Down
5 changes: 2 additions & 3 deletions tests/test35.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/test56.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test66.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test83.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/test84.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 1048ed8

Please sign in to comment.