Skip to content

Commit 10c93b2

Browse files
committed
Ensure that strings copied by strncpy() are NUL-terminated
strncpy() does not guarantee that the destination buffer will be NUL-terminated, so this change ensures that by explicitly setting the last element to NUL. POSIX.1-2024 defines the function strlcpy() for that purpose, but given the standard's freshness and the limited number of occurrences in ROBODoc's source code, it was easier just to amend the few uses.
1 parent 7b24ca1 commit 10c93b2

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

Source/robohdrs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,13 @@ cmdLine( int argc, char **argv )
301301
case 't':
302302
/* specify version control tag */
303303
strncpy( vcTag, optarg, MAXNAME );
304+
vcTag[sizeof(vcTag) - 1] = '\0';
304305
break;
305306

306307
case 'p':
307308
/* specify project name */
308309
strncpy( projName, optarg, MAXNAME );
310+
projName[sizeof(projName) - 1] = '\0';
309311
break;
310312

311313
case 'i':
@@ -329,6 +331,7 @@ cmdLine( int argc, char **argv )
329331
c = c->next = nc;
330332
}
331333
strncpy( c->name, optarg, MAXNAME );
334+
c->name[sizeof(c->name) - 1] = '\0';
332335
break;
333336

334337
case 'l':
@@ -370,6 +373,7 @@ cmdLine( int argc, char **argv )
370373

371374
case 'x':
372375
strncpy( ctagsBin, optarg, MAXNAME );
376+
ctagsBin[sizeof(ctagsBin) - 1] = '\0';
373377
break;
374378

375379
case '?':
@@ -559,9 +563,13 @@ addList( ctags_t * e, char *fname, char *name, char *decl, char *type,
559563
e->cnt++;
560564

561565
strncpy( ctag->fname, fname, MAXNAME );
566+
ctag->fname[sizeof(ctag->fname) - 1] = '\0';
562567
strncpy( ctag->name, name, MAXNAME );
568+
ctag->name[sizeof(ctag->name) - 1] = '\0';
563569
strncpy( ctag->decl, decl, MAXLINE );
570+
ctag->decl[sizeof(ctag->decl) - 1] = '\0';
564571
strncpy( ctag->type, type, MAXNAME );
572+
ctag->type[sizeof(ctag->type) - 1] = '\0';
565573
ctag->linenum = linenum;
566574
}
567575

0 commit comments

Comments
 (0)