SCP: Fix Coverity identified warning and migrate away from strncpy to strlcpy

This commit is contained in:
Mark Pizzolato 2017-10-19 20:04:08 -07:00
parent 6d1e873087
commit 5b248cf7f4
2 changed files with 25 additions and 24 deletions

30
scp.c
View file

@ -2323,7 +2323,7 @@ if ((!cptr) || (*cptr == '\0'))
cptr = get_glyph_nc (cptr, gbuf, '"'); /* get quote delimited token */ cptr = get_glyph_nc (cptr, gbuf, '"'); /* get quote delimited token */
if (gbuf[0] == '\0') { /* Token started with quote */ if (gbuf[0] == '\0') { /* Token started with quote */
gbuf[sizeof (gbuf)-1] = '\0'; gbuf[sizeof (gbuf)-1] = '\0';
strncpy (gbuf, cptr, sizeof (gbuf)-1); strlcpy (gbuf, cptr, sizeof (gbuf));
gptr = strchr (gbuf, '"'); gptr = strchr (gbuf, '"');
if (gptr) if (gptr)
*gptr = '\0'; *gptr = '\0';
@ -3001,7 +3001,7 @@ sim_on_inherit =(sim_switches & SWMASK ('O')) || sim_on_inherit; /* -o means inh
errabort = sim_switches & SWMASK ('E'); /* -e means abort on error */ errabort = sim_switches & SWMASK ('E'); /* -e means abort on error */
abuf[sizeof(abuf)-1] = '\0'; abuf[sizeof(abuf)-1] = '\0';
strncpy (abuf, fcptr, sizeof(abuf)-1); strlcpy (abuf, fcptr, sizeof(abuf));
c = abuf; c = abuf;
do_arg[10] = NULL; /* make sure the argument list always ends with a NULL */ do_arg[10] = NULL; /* make sure the argument list always ends with a NULL */
for (nargs = 0; nargs < 10; ) { /* extract arguments */ for (nargs = 0; nargs < 10; ) { /* extract arguments */
@ -5235,7 +5235,7 @@ if (sim_is_running)
if ((!cptr) || (*cptr == 0)) if ((!cptr) || (*cptr == 0))
return SCPE_2FARG; return SCPE_2FARG;
gbuf[sizeof(gbuf)-1] = '\0'; gbuf[sizeof(gbuf)-1] = '\0';
strncpy (gbuf, cptr, sizeof(gbuf)-1); strlcpy (gbuf, cptr, sizeof(gbuf));
sim_trim_endspc(gbuf); sim_trim_endspc(gbuf);
if (chdir(gbuf) != 0) if (chdir(gbuf) != 0)
return sim_messagef(SCPE_IOERR, "Unable to directory change to: %s\n", gbuf); return sim_messagef(SCPE_IOERR, "Unable to directory change to: %s\n", gbuf);
@ -5531,7 +5531,7 @@ char lbuf[4*CBUFSIZE];
if ((!cptr) || (*cptr == 0)) if ((!cptr) || (*cptr == 0))
return SCPE_2FARG; return SCPE_2FARG;
lbuf[sizeof(lbuf)-1] = '\0'; lbuf[sizeof(lbuf)-1] = '\0';
strncpy (lbuf, cptr, sizeof(lbuf)-1); strlcpy (lbuf, cptr, sizeof(lbuf));
sim_trim_endspc(lbuf); sim_trim_endspc(lbuf);
file = sim_fopen (lbuf, "r"); file = sim_fopen (lbuf, "r");
if (file == NULL) { /* open failed? */ if (file == NULL) { /* open failed? */
@ -5668,7 +5668,7 @@ if (uptr == NULL)
return SCPE_IERR; return SCPE_IERR;
max = uptr->capac - 1; max = uptr->capac - 1;
abuf[sizeof(abuf)-1] = '\0'; abuf[sizeof(abuf)-1] = '\0';
strncpy (abuf, cptr, sizeof(abuf)-1); strlcpy (abuf, cptr, sizeof(abuf));
if ((aptr = strchr (abuf, ';'))) { /* ;action? */ if ((aptr = strchr (abuf, ';'))) { /* ;action? */
cptr += aptr - abuf + 1; cptr += aptr - abuf + 1;
if (flg != SSH_ST) /* only on SET */ if (flg != SSH_ST) /* only on SET */
@ -5919,7 +5919,7 @@ if (uptr->flags & UNIT_ATT) { /* already attached? */
} }
} }
gbuf[sizeof(gbuf)-1] = '\0'; gbuf[sizeof(gbuf)-1] = '\0';
strncpy (gbuf, cptr, sizeof(gbuf)-1); strlcpy (gbuf, cptr, sizeof(gbuf));
sim_trim_endspc (gbuf); /* trim trailing spc */ sim_trim_endspc (gbuf); /* trim trailing spc */
return scp_attach_unit (dptr, uptr, gbuf); /* attach */ return scp_attach_unit (dptr, uptr, gbuf); /* attach */
} }
@ -5948,7 +5948,7 @@ if ((dptr = find_dev_from_unit (uptr)) == NULL)
uptr->filename = (char *) calloc (CBUFSIZE, sizeof (char)); /* alloc name buf */ uptr->filename = (char *) calloc (CBUFSIZE, sizeof (char)); /* alloc name buf */
if (uptr->filename == NULL) if (uptr->filename == NULL)
return SCPE_MEM; return SCPE_MEM;
strncpy (uptr->filename, cptr, CBUFSIZE); /* save name */ strlcpy (uptr->filename, cptr, CBUFSIZE); /* save name */
if ((sim_switches & SWMASK ('R')) || /* read only? */ if ((sim_switches & SWMASK ('R')) || /* read only? */
((uptr->flags & UNIT_RO) != 0)) { ((uptr->flags & UNIT_RO) != 0)) {
if (((uptr->flags & UNIT_ROABLE) == 0) && /* allowed? */ if (((uptr->flags & UNIT_ROABLE) == 0) && /* allowed? */
@ -6263,7 +6263,7 @@ GET_SWITCHES (cptr); /* get switches */
if (*cptr == 0) /* must be more */ if (*cptr == 0) /* must be more */
return SCPE_2FARG; return SCPE_2FARG;
gbuf[sizeof(gbuf)-1] = '\0'; gbuf[sizeof(gbuf)-1] = '\0';
strncpy (gbuf, cptr, sizeof(gbuf)-1); strlcpy (gbuf, cptr, sizeof(gbuf));
sim_trim_endspc (gbuf); sim_trim_endspc (gbuf);
if ((sfile = sim_fopen (gbuf, "wb")) == NULL) if ((sfile = sim_fopen (gbuf, "wb")) == NULL)
return SCPE_OPENERR; return SCPE_OPENERR;
@ -6418,7 +6418,7 @@ GET_SWITCHES (cptr); /* get switches */
if (*cptr == 0) /* must be more */ if (*cptr == 0) /* must be more */
return SCPE_2FARG; return SCPE_2FARG;
gbuf[sizeof(gbuf)-1] = '\0'; gbuf[sizeof(gbuf)-1] = '\0';
strncpy (gbuf, cptr, sizeof(gbuf)-1); strlcpy (gbuf, cptr, sizeof(gbuf));
sim_trim_endspc (gbuf); sim_trim_endspc (gbuf);
if ((rfile = sim_fopen (gbuf, "rb")) == NULL) if ((rfile = sim_fopen (gbuf, "rb")) == NULL)
return SCPE_OPENERR; return SCPE_OPENERR;
@ -7939,7 +7939,7 @@ if (prompt) { /* interactive? */
if (tmpc == NULL) /* bad result? */ if (tmpc == NULL) /* bad result? */
cptr = NULL; cptr = NULL;
else { else {
strncpy (cptr, tmpc, size); /* copy result */ strlcpy (cptr, tmpc, size); /* copy result */
free (tmpc) ; /* free temp */ free (tmpc) ; /* free temp */
} }
} }
@ -10114,7 +10114,7 @@ if ((act != NULL) && (*act != 0)) { /* new action? */
char *newp = (char *) calloc (CBUFSIZE+1, sizeof (char)); /* alloc buf */ char *newp = (char *) calloc (CBUFSIZE+1, sizeof (char)); /* alloc buf */
if (newp == NULL) /* mem err? */ if (newp == NULL) /* mem err? */
return SCPE_MEM; return SCPE_MEM;
strncpy (newp, act, CBUFSIZE); /* copy action */ strlcpy (newp, act, CBUFSIZE); /* copy action */
bp->act = newp; /* set pointer */ bp->act = newp; /* set pointer */
} }
sim_brk_summ = sim_brk_summ | (sw & ~BRK_TYP_TEMP); sim_brk_summ = sim_brk_summ | (sw & ~BRK_TYP_TEMP);
@ -10342,7 +10342,7 @@ if ((ep = strchr (sim_brk_act[sim_do_depth], ';'))) { /* cmd delimiter? */
sim_brk_act[sim_do_depth] += lnt + 1; /* adv ptr */ sim_brk_act[sim_do_depth] += lnt + 1; /* adv ptr */
} }
else { else {
strncpy (buf, sim_brk_act[sim_do_depth], size); /* copy action */ strlcpy (buf, sim_brk_act[sim_do_depth], size); /* copy action */
sim_brk_clract (); /* no more */ sim_brk_clract (); /* no more */
} }
return buf; return buf;
@ -10729,7 +10729,7 @@ return SCPE_OK;
t_stat sim_exp_show_tab (FILE *st, const EXPECT *exp, const EXPTAB *ep) t_stat sim_exp_show_tab (FILE *st, const EXPECT *exp, const EXPTAB *ep)
{ {
const char *dev_name = dev_name = tmxr_expect_line_name (exp); const char *dev_name = tmxr_expect_line_name (exp);
uint32 default_haltafter = get_default_env_parameter (dev_name, "SIM_EXPECT_HALTAFTER", 0); uint32 default_haltafter = get_default_env_parameter (dev_name, "SIM_EXPECT_HALTAFTER", 0);
if (!ep) if (!ep)
@ -10757,7 +10757,7 @@ return SCPE_OK;
t_stat sim_exp_show (FILE *st, CONST EXPECT *exp, const char *match) t_stat sim_exp_show (FILE *st, CONST EXPECT *exp, const char *match)
{ {
CONST EXPTAB *ep = (CONST EXPTAB *)sim_exp_fnd (exp, match, 0); CONST EXPTAB *ep = (CONST EXPTAB *)sim_exp_fnd (exp, match, 0);
const char *dev_name = dev_name = tmxr_expect_line_name (exp); const char *dev_name = tmxr_expect_line_name (exp);
uint32 default_haltafter = get_default_env_parameter (dev_name, "SIM_EXPECT_HALTAFTER", 0); uint32 default_haltafter = get_default_env_parameter (dev_name, "SIM_EXPECT_HALTAFTER", 0);
if (exp->buf_size) { if (exp->buf_size) {
@ -12419,7 +12419,7 @@ if (fp == NULL) {
* of the executable. Failing that, we're out of luck. * of the executable. Failing that, we're out of luck.
*/ */
fbuf[sizeof(fbuf)-1] = '\0'; fbuf[sizeof(fbuf)-1] = '\0';
strncpy (fbuf, sim_argv[0], sizeof (fbuf)-1); strlcpy (fbuf, sim_argv[0], sizeof (fbuf));
if ((p = (char *)match_ext (fbuf, "EXE"))) if ((p = (char *)match_ext (fbuf, "EXE")))
*p = '\0'; *p = '\0';
if ((p = strrchr (fbuf, '\\'))) { if ((p = strrchr (fbuf, '\\'))) {

View file

@ -2448,7 +2448,7 @@ while (*tptr) {
if (0 == MATCH_CMD (gbuf, "LOG")) { if (0 == MATCH_CMD (gbuf, "LOG")) {
if ((NULL == cptr) || ('\0' == *cptr)) if ((NULL == cptr) || ('\0' == *cptr))
return sim_messagef (SCPE_2FARG, "Missing Log Specifier\n"); return sim_messagef (SCPE_2FARG, "Missing Log Specifier\n");
strncpy(logfiletmpl, cptr, sizeof(logfiletmpl)-1); strlcpy(logfiletmpl, cptr, sizeof(logfiletmpl));
continue; continue;
} }
if (0 == MATCH_CMD (gbuf, "LOOPBACK")) { if (0 == MATCH_CMD (gbuf, "LOOPBACK")) {
@ -2514,14 +2514,14 @@ while (*tptr) {
if (0 == MATCH_CMD (gbuf, "CONNECT")) { if (0 == MATCH_CMD (gbuf, "CONNECT")) {
if ((NULL == cptr) || ('\0' == *cptr)) if ((NULL == cptr) || ('\0' == *cptr))
return sim_messagef (SCPE_2FARG, "Missing Connect Specifier\n"); return sim_messagef (SCPE_2FARG, "Missing Connect Specifier\n");
strncpy (destination, cptr, sizeof(destination)-1); strlcpy (destination, cptr, sizeof(destination));
continue; continue;
} }
if (0 == MATCH_CMD (gbuf, "SPEED")) { if (0 == MATCH_CMD (gbuf, "SPEED")) {
if ((NULL == cptr) || ('\0' == *cptr) || if ((NULL == cptr) || ('\0' == *cptr) ||
(_tmln_speed_delta (cptr) < 0)) (_tmln_speed_delta (cptr) < 0))
return sim_messagef (SCPE_ARG, "Invalid Speed Specifier: %s\n", (cptr ? cptr : "")); return sim_messagef (SCPE_ARG, "Invalid Speed Specifier: %s\n", (cptr ? cptr : ""));
strncpy (speed, cptr, sizeof(speed)-1); strlcpy (speed, cptr, sizeof(speed));
continue; continue;
} }
cptr = get_glyph (gbuf, port, ';'); cptr = get_glyph (gbuf, port, ';');
@ -2572,7 +2572,7 @@ while (*tptr) {
char *eptr; char *eptr;
memset (hostport, '\0', sizeof(hostport)); memset (hostport, '\0', sizeof(hostport));
strncpy (hostport, destination, sizeof(hostport)-1); strlcpy (hostport, destination, sizeof(hostport));
if ((eptr = strchr (hostport, ';'))) if ((eptr = strchr (hostport, ';')))
*(eptr++) = '\0'; *(eptr++) = '\0';
if (eptr) { if (eptr) {
@ -2599,16 +2599,17 @@ while (*tptr) {
if (modem_control != mp->modem_control) if (modem_control != mp->modem_control)
return SCPE_ARG; return SCPE_ARG;
if (logfiletmpl[0]) { if (logfiletmpl[0]) {
strncpy(mp->logfiletmpl, logfiletmpl, sizeof(mp->logfiletmpl)-1); strlcpy(mp->logfiletmpl, logfiletmpl, sizeof(mp->logfiletmpl));
for (i = 0; i < mp->lines; i++) { for (i = 0; i < mp->lines; i++) {
lp = mp->ldsc + i; lp = mp->ldsc + i;
sim_close_logfile (&lp->txlogref); sim_close_logfile (&lp->txlogref);
lp->txlog = NULL; lp->txlog = NULL;
lp->txlogname = (char *)realloc(lp->txlogname, CBUFSIZE); lp->txlogname = (char *)realloc(lp->txlogname, CBUFSIZE);
lp->txlogname[CBUFSIZE-1] = '\0';
if (mp->lines > 1) if (mp->lines > 1)
sprintf(lp->txlogname, "%s_%d", mp->logfiletmpl, i); snprintf(lp->txlogname, CBUFSIZE-1, "%s_%d", mp->logfiletmpl, i);
else else
strcpy (lp->txlogname, mp->logfiletmpl); strlcpy (lp->txlogname, mp->logfiletmpl, CBUFSIZE);
r = sim_open_logfile (lp->txlogname, TRUE, &lp->txlog, &lp->txlogref); r = sim_open_logfile (lp->txlogname, TRUE, &lp->txlog, &lp->txlogref);
if (r != SCPE_OK) { if (r != SCPE_OK) {
free (lp->txlogname); free (lp->txlogname);
@ -3624,7 +3625,7 @@ for (i=0; i<tmxr_open_device_count; ++i)
if (tmxr_open_devices[i]->lines > 1) if (tmxr_open_devices[i]->lines > 1)
snprintf (line_name, sizeof (line_name), "%s:%d", tmxr_open_devices[i]->ldsc[j].send.dptr->name, j); snprintf (line_name, sizeof (line_name), "%s:%d", tmxr_open_devices[i]->ldsc[j].send.dptr->name, j);
else else
strncpy (line_name, tmxr_open_devices[i]->ldsc[j].send.dptr->name, sizeof (line_name)); strlcpy (line_name, tmxr_open_devices[i]->ldsc[j].send.dptr->name, sizeof (line_name));
break; break;
} }
return line_name; return line_name;
@ -4477,7 +4478,7 @@ if (lp->txlog) /* close existing log */
lp->txlogname = (char *) calloc (CBUFSIZE, sizeof (char)); /* alloc namebuf */ lp->txlogname = (char *) calloc (CBUFSIZE, sizeof (char)); /* alloc namebuf */
if (lp->txlogname == NULL) /* can't? */ if (lp->txlogname == NULL) /* can't? */
return SCPE_MEM; return SCPE_MEM;
strncpy (lp->txlogname, cptr, CBUFSIZE); /* save file name */ strlcpy (lp->txlogname, cptr, CBUFSIZE); /* save file name */
sim_open_logfile (cptr, TRUE, &lp->txlog, &lp->txlogref);/* open log */ sim_open_logfile (cptr, TRUE, &lp->txlog, &lp->txlogref);/* open log */
if (lp->txlog == NULL) { /* error? */ if (lp->txlog == NULL) { /* error? */
free (lp->txlogname); /* free buffer */ free (lp->txlogname); /* free buffer */