Handle snprintf() returning -1.

MFC after:	2 weeks
This commit is contained in:
brian 2001-08-20 13:24:39 +00:00
parent 587f3f71be
commit f81c407f1b
9 changed files with 62 additions and 54 deletions

View File

@ -164,7 +164,11 @@ get_slot_info(int so, int slot, char **manuf, char **version, char
char *_version;
char *_device;
slen = snprintf(buf, sizeof buf, "N%d", slot);
if ((slen = snprintf(buf, sizeof buf, "N%d", slot)) == -1) {
warnc(0, "write");
goto err;
}
if ((rv = write(so, buf, slen)) < 0) {
warn("write");
goto err;

View File

@ -443,8 +443,9 @@ updateproctitle(struct disk *disks) {
p += sizeof _PATH_DEV - 1;
percent = 100 * (double)dseek(dp, 0, SEEK_CUR) / dp->size;
if ((size_t)(ret = snprintf(bp, size,
"%s %.2f%%, ", p, percent)) >= size) {
if ((ret = snprintf(bp, size, "%s %.2f%%, ", p, percent)) == -1)
ret = 0;
if ((size_t)ret >= size) {
inc = ((ret + 1023) >> 10) << 10;
size += inc;
bufsize += inc;
@ -456,6 +457,8 @@ updateproctitle(struct disk *disks) {
}
bp = buf + bufsize - size;
ret = snprintf(bp, size, "%s %.2f%%, ", p, percent);
if (ret == -1)
ret = 0;
}
bp += ret;
size -= ret;

View File

@ -410,8 +410,7 @@ ftp_copyresult(int src, int dst, enum state state)
"425 Cannot open data connetion\r\n");
}
}
write(dst, rbuf, n);
return n;
return n > 0 ? write(dst, rbuf, n) : n;
case LPRT:
case EPRT:
/* expecting "200 PORT command successful." */
@ -505,8 +504,7 @@ ftp_copyresult(int src, int dst, enum state state)
passivefail:
n = snprintf(sbuf, sizeof(sbuf),
"500 could not translate from PASV\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
#ifdef IPV6_FAITH
{
@ -566,14 +564,16 @@ ftp_copyresult(int src, int dst, enum state state)
UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
2, UC(p[0]), UC(p[1]));
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
passivemode = 1;
return n;
} else {
n = snprintf(sbuf, sizeof(sbuf),
"229 Entering Extended Passive Mode (|||%d|)\r\n",
ntohs(sin6->sin6_port));
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
passivemode = 1;
return n;
}
@ -626,8 +626,7 @@ ftp_copyresult(int src, int dst, enum state state)
passivefail2:
n = snprintf(sbuf, sizeof(sbuf),
"500 could not translate from EPSV\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
#ifdef IP_FAITH
{
@ -684,7 +683,8 @@ ftp_copyresult(int src, int dst, enum state state)
"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
UC(p[0]), UC(p[1]));
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
passivemode = 1;
return n;
}
@ -786,8 +786,7 @@ ftp_copycommand(int src, int dst, enum state *state)
if (epsvall) {
n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
cmd);
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
n = sscanf(param,
@ -800,8 +799,7 @@ ftp_copycommand(int src, int dst, enum state *state)
if (n != 21 || af != 6 || hal != 16|| pal != 2) {
n = snprintf(sbuf, sizeof(sbuf),
"501 illegal parameter to LPRT\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
/* keep LPRT parameter */
@ -821,8 +819,7 @@ ftp_copycommand(int src, int dst, enum state *state)
lprtfail:
n = snprintf(sbuf, sizeof(sbuf),
"500 could not translate to PORT\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
if (((struct sockaddr *)&data4)->sa_family != AF_INET)
goto lprtfail;
@ -863,7 +860,8 @@ ftp_copycommand(int src, int dst, enum state *state)
n = snprintf(sbuf, sizeof(sbuf), "PORT %d,%d,%d,%d,%d,%d\r\n",
UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
UC(p[0]), UC(p[1]));
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
*state = nstate;
passivemode = 0;
return n;
@ -885,8 +883,7 @@ ftp_copycommand(int src, int dst, enum state *state)
if (epsvall) {
n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
cmd);
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
p = param;
@ -898,8 +895,7 @@ ftp_copycommand(int src, int dst, enum state *state)
eprtparamfail:
n = snprintf(sbuf, sizeof(sbuf),
"501 illegal parameter to EPRT\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
*p++ = '\0';
hostp = p;
@ -919,8 +915,7 @@ ftp_copycommand(int src, int dst, enum state *state)
if (n != 1 || af != 2) {
n = snprintf(sbuf, sizeof(sbuf),
"501 unsupported address family to EPRT\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
@ -929,14 +924,12 @@ ftp_copycommand(int src, int dst, enum state *state)
if (error) {
n = snprintf(sbuf, sizeof(sbuf),
"501 EPRT: %s\r\n", gai_strerror(error));
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
if (res->ai_next) {
n = snprintf(sbuf, sizeof(sbuf),
"501 EPRT: %s resolved to multiple addresses\r\n", hostp);
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
memcpy(&data6, res->ai_addr, res->ai_addrlen);
@ -957,13 +950,13 @@ ftp_copycommand(int src, int dst, enum state *state)
if (epsvall) {
n = snprintf(sbuf, sizeof(sbuf), "501 %s disallowed in EPSV ALL\r\n",
cmd);
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
/* transmit PASV */
n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
*state = LPSV;
passivemode = 0; /* to be set to 1 later */
return n;
@ -978,7 +971,8 @@ ftp_copycommand(int src, int dst, enum state *state)
wport4 = wport6 = port4 = port6 = -1;
n = snprintf(sbuf, sizeof(sbuf), "PASV\r\n");
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
*state = EPSV;
passivemode = 0; /* to be set to 1 later */
return n;
@ -989,8 +983,7 @@ ftp_copycommand(int src, int dst, enum state *state)
*/
epsvall = 1;
n = snprintf(sbuf, sizeof(sbuf), "200 EPSV ALL command successful.\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
#ifdef FAITH4
} else if (strcmp(cmd, "PORT") == 0 && param) {
/*
@ -1012,8 +1005,7 @@ ftp_copycommand(int src, int dst, enum state *state)
if (n != 6) {
n = snprintf(sbuf, sizeof(sbuf),
"501 illegal parameter to PORT\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
memset(&data6, 0, sizeof(data6));
@ -1032,8 +1024,7 @@ ftp_copycommand(int src, int dst, enum state *state)
portfail:
n = snprintf(sbuf, sizeof(sbuf),
"500 could not translate to EPRT\r\n");
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
}
if (((struct sockaddr *)&data4)->sa_family != AF_INET6)
goto portfail;
@ -1073,7 +1064,8 @@ ftp_copycommand(int src, int dst, enum state *state)
goto portfail;
}
n = snprintf(sbuf, sizeof(sbuf), "EPRT |%d|%s|%s|\r\n", af, host, serv);
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
*state = nstate;
passivemode = 0;
return n;
@ -1092,7 +1084,8 @@ ftp_copycommand(int src, int dst, enum state *state)
/* transmit EPSV */
n = snprintf(sbuf, sizeof(sbuf), "EPSV\r\n");
write(dst, sbuf, n);
if (n > 0)
n = write(dst, sbuf, n);
*state = PASV;
passivemode = 0; /* to be set to 1 later */
return n;
@ -1102,8 +1095,7 @@ ftp_copycommand(int src, int dst, enum state *state)
* reject PORT/PASV
*/
n = snprintf(sbuf, sizeof(sbuf), "502 %s not implemented.\r\n", cmd);
write(src, sbuf, n);
return n;
return n > 0 ? write(src, sbuf, n) : n;
#endif /* FAITH4 */
} else if (passivemode
&& (strcmp(cmd, "STOR") == 0
@ -1119,7 +1111,8 @@ ftp_copycommand(int src, int dst, enum state *state)
if (ftp_passiveconn() < 0) {
n = snprintf(sbuf, sizeof(sbuf), "425 Cannot open data connetion\r\n");
write(src, sbuf, n);
if (n > 0)
n = write(src, sbuf, n);
} else {
/* simply relay the command */
write(dst, rbuf, n);
@ -1130,8 +1123,7 @@ ftp_copycommand(int src, int dst, enum state *state)
} else {
/* simply relay it */
*state = NONE;
write(dst, rbuf, n);
return n;
return write(dst, rbuf, n);
}
bad:

View File

@ -158,7 +158,7 @@ extract_plist(char *home, Package *pkg)
PUSHOUT(Directory);
}
add_count = snprintf(&perm_args[perm_count], maxargs - perm_count, "'%s' ", p->name);
if (add_count > maxargs - perm_count) {
if (add_count == -1 || add_count > maxargs - perm_count) {
cleanup(0);
errx(2, __FUNCTION__ ": oops, miscounted strings!");
}
@ -178,7 +178,7 @@ extract_plist(char *home, Package *pkg)
PUSHOUT(Directory);
}
add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name);
if (add_count > maxargs - where_count) {
if (add_count == -1 || add_count > maxargs - where_count) {
cleanup(0);
errx(2, __FUNCTION__ ": oops, miscounted strings!");
}
@ -186,7 +186,7 @@ extract_plist(char *home, Package *pkg)
add_count = snprintf(&perm_args[perm_count],
maxargs - perm_count,
"'%s' ", p->name);
if (add_count > maxargs - perm_count) {
if (add_count == -1 || add_count > maxargs - perm_count) {
cleanup(0);
errx(2, __FUNCTION__ ": oops, miscounted strings!");
}

View File

@ -190,7 +190,7 @@ copy_plist(char *home, Package *plist)
p->name);
last_chdir = home;
}
if (add_count > maxargs - where_count) {
if (add_count == -1 || add_count > maxargs - where_count) {
cleanup(0);
errx(2, __FUNCTION__ ": oops, miscounted strings!");
}
@ -228,7 +228,7 @@ copy_plist(char *home, Package *plist)
" -C %s %s",
mythere ? mythere : where,
p->name);
if (add_count > maxargs - where_count) {
if (add_count == -1 || add_count > maxargs - where_count) {
cleanup(0);
errx(2, __FUNCTION__ ": oops, miscounted strings!");
}

View File

@ -134,7 +134,7 @@ gr_update(struct group * grp, char const * group, int mode)
char *grbuf = NULL;
ENDGRENT();
l = snprintf(pfx, sizeof pfx, "%s:", group);
snprintf(pfx, sizeof pfx, "%s:", group);
/*
* Update the group file

View File

@ -420,6 +420,8 @@ write_userconfig(char const * file)
for (j = k = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) {
char lbuf[64];
int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", system_shells[j]);
if (l == -1)
l = 0;
if (l + k + 1 < len || extendline(&buf, &len, len + LNBUFSZ) != -1) {
strcpy(buf + k, lbuf);
k += l;
@ -438,6 +440,8 @@ write_userconfig(char const * file)
for (j = k = 0; j < config.numgroups && config.groups[j] != NULL; j++) {
char lbuf[64];
int l = snprintf(lbuf, sizeof lbuf, "%s\"%s\"", k ? "," : "", config.groups[j]);
if (l == -1)
l = 0;
if (l + k + 1 < len || extendline(&buf, &len, len + 1024) != -1) {
strcpy(buf + k, lbuf);
k += l;

View File

@ -166,6 +166,8 @@ pw_update(struct passwd * pwd, char const * user, int mode)
else
fmtpwentry(pwbuf, pwd, PWF_PASSWD);
if (l == -1)
l = 0;
rc = fileupdate(getpwpath(_PASSWD), 0644, pwbuf, pfx, l, mode);
if (rc == 0) {

View File

@ -901,7 +901,8 @@ fprintlog(f, flags, msg)
v->iov_len = snprintf(greetings, sizeof greetings,
"\r\n\7Message from syslogd@%s at %.24s ...\r\n",
f->f_prevhost, ctime(&now));
v++;
if (v->iov_len > 0)
v++;
v->iov_base = "";
v->iov_len = 0;
v++;
@ -995,7 +996,9 @@ fprintlog(f, flags, msg)
else
l = snprintf(line, sizeof line - 1, "<%d>%.15s %s",
f->f_prevpri, iov[0].iov_base, iov[5].iov_base);
if (l > MAXLINE)
if (l < 0)
l = 0;
else if (l > MAXLINE)
l = MAXLINE;
if (finet) {