Use strdup in snmp_parse_server(..) when possible instead of malloc+strcpy

This simplifies the code and mutes a Coverity warning about sc->cport being
improperly allocated

Reported by:	Coverity
CID:		1018247
MFC after:	1 week
This commit is contained in:
Enji Cooper 2016-12-31 11:13:00 +00:00
parent 6ca28febf6
commit bfb81e6524
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=310931

View File

@ -1937,20 +1937,18 @@ snmp_parse_server(struct snmp_client *sc, const char *str)
}
/* port */
free(sc->cport);
if ((sc->cport = malloc(strlen(p + 1) + 1)) == NULL) {
if ((sc->cport = strdup(p + 1)) == NULL) {
seterr(sc, "%s", strerror(errno));
return (-1);
}
strcpy(sc->cport, p + 1);
} else if (p > s) {
/* host */
free(sc->chost);
if ((sc->chost = malloc(strlen(s) + 1)) == NULL) {
if ((sc->chost = strdup(strlen(s))) == NULL) {
seterr(sc, "%s", strerror(errno));
return (-1);
}
strcpy(sc->chost, s);
}
return (0);
}