@@ -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,22 +1618,45 @@ 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+ /* someone passed a "@hostname" string? */
1622+ if (s == tmp ) {
1623+ fprintf (stderr , "upscli_splitname: got empty upsname string\n" );
16231624 return -1 ;
16241625 }
16251626
1627+ if ((* upsname = xstrdup (strtok_r (tmp , "@" , & last ))) == NULL ) {
1628+ fprintf (stderr , "upscli_splitname: xstrdup failed\n" );
1629+ return -1 ;
1630+ }
1631+
1632+ /* someone passed a "@hostname" string (take two)? */
1633+ if (!* * upsname ) {
1634+ fprintf (stderr , "upscli_splitname: got empty upsname string\n" );
1635+ return -1 ;
1636+ }
1637+
1638+ /*
1639+ fprintf(stderr, "upscli_splitname3: got buf='%s', tmp='%s', upsname='%s', possible hostname:port='%s'\n",
1640+ NUT_STRARG(buf), NUT_STRARG(tmp), NUT_STRARG(*upsname), NUT_STRARG((s ? s+1 : s)));
1641+ */
1642+
16261643 /* only a upsname is specified, fill in defaults */
16271644 if (s == NULL ) {
1628- if ((* hostname = strdup ("localhost" )) == NULL ) {
1629- fprintf (stderr , "upscli_splitname: strdup failed\n" );
1645+ if ((* hostname = xstrdup ("localhost" )) == NULL ) {
1646+ fprintf (stderr , "upscli_splitname: xstrdup failed\n" );
16301647 return -1 ;
16311648 }
16321649
16331650 * port = PORT ;
16341651 return 0 ;
16351652 }
16361653
1654+ /* someone passed a "upsname@" string? */
1655+ if (!(* (s + 1 ))) {
1656+ fprintf (stderr , "upscli_splitname: got the @ separator and then an empty hostname[:port] string\n" );
1657+ return -1 ;
1658+ }
1659+
16371660 return upscli_splitaddr (s + 1 , hostname , port );
16381661}
16391662
@@ -1659,8 +1682,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
16591682 return -1 ;
16601683 }
16611684
1662- if ((* hostname = strdup (strtok_r (tmp + 1 , "]" , & last ))) == NULL ) {
1663- fprintf (stderr , "upscli_splitaddr: strdup failed\n" );
1685+ if ((* hostname = xstrdup (strtok_r (tmp + 1 , "]" , & last ))) == NULL ) {
1686+ fprintf (stderr , "upscli_splitaddr: xstrdup failed\n" );
16641687 return -1 ;
16651688 }
16661689
@@ -1672,8 +1695,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
16721695 } else {
16731696 s = strchr (tmp , ':' );
16741697
1675- if ((* hostname = strdup (strtok_r (tmp , ":" , & last ))) == NULL ) {
1676- fprintf (stderr , "upscli_splitaddr: strdup failed\n" );
1698+ if ((* hostname = xstrdup (strtok_r (tmp , ":" , & last ))) == NULL ) {
1699+ fprintf (stderr , "upscli_splitaddr: xstrdup failed\n" );
16771700 return -1 ;
16781701 }
16791702
0 commit comments