Skip to content

Commit fde7672

Browse files
committed
clients/upsclient.c: fix strdup()=>xstrdup() to not segfault with bad inputs [#2052]
Signed-off-by: Jim Klimov <[email protected]>
1 parent 05610d1 commit fde7672

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

clients/upsclient.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ int upscli_tryconnect(UPSCONN_t *ups, const char *host, uint16_t port, int flags
11611161

11621162
pconf_init(&ups->pc_ctx, NULL);
11631163

1164-
ups->host = strdup(host);
1164+
ups->host = xstrdup(host);
11651165

11661166
if (!ups->host) {
11671167
ups->upserror = UPSCLI_ERR_NOMEM;
@@ -1618,15 +1618,15 @@ int upscli_splitname(const char *buf, char **upsname, char **hostname, uint16_t
16181618

16191619
s = strchr(tmp, '@');
16201620

1621-
if ((*upsname = strdup(strtok_r(tmp, "@", &last))) == NULL) {
1622-
fprintf(stderr, "upscli_splitname: strdup failed\n");
1621+
if ((*upsname = xstrdup(strtok_r(tmp, "@", &last))) == NULL) {
1622+
fprintf(stderr, "upscli_splitname: xstrdup failed\n");
16231623
return -1;
16241624
}
16251625

16261626
/* only a upsname is specified, fill in defaults */
16271627
if (s == NULL) {
1628-
if ((*hostname = strdup("localhost")) == NULL) {
1629-
fprintf(stderr, "upscli_splitname: strdup failed\n");
1628+
if ((*hostname = xstrdup("localhost")) == NULL) {
1629+
fprintf(stderr, "upscli_splitname: xstrdup failed\n");
16301630
return -1;
16311631
}
16321632

@@ -1659,8 +1659,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
16591659
return -1;
16601660
}
16611661

1662-
if ((*hostname = strdup(strtok_r(tmp+1, "]", &last))) == NULL) {
1663-
fprintf(stderr, "upscli_splitaddr: strdup failed\n");
1662+
if ((*hostname = xstrdup(strtok_r(tmp+1, "]", &last))) == NULL) {
1663+
fprintf(stderr, "upscli_splitaddr: xstrdup failed\n");
16641664
return -1;
16651665
}
16661666

@@ -1672,8 +1672,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
16721672
} else {
16731673
s = strchr(tmp, ':');
16741674

1675-
if ((*hostname = strdup(strtok_r(tmp, ":", &last))) == NULL) {
1676-
fprintf(stderr, "upscli_splitaddr: strdup failed\n");
1675+
if ((*hostname = xstrdup(strtok_r(tmp, ":", &last))) == NULL) {
1676+
fprintf(stderr, "upscli_splitaddr: xstrdup failed\n");
16771677
return -1;
16781678
}
16791679

0 commit comments

Comments
 (0)