@@ -1161,7 +1161,7 @@ int upscli_tryconnect(UPSCONN_t *ups, const char *host, uint16_t port, int flags
1161
1161
1162
1162
pconf_init (& ups -> pc_ctx , NULL );
1163
1163
1164
- ups -> host = strdup (host );
1164
+ ups -> host = xstrdup (host );
1165
1165
1166
1166
if (!ups -> host ) {
1167
1167
ups -> upserror = UPSCLI_ERR_NOMEM ;
@@ -1618,22 +1618,45 @@ int upscli_splitname(const char *buf, char **upsname, char **hostname, uint16_t
1618
1618
1619
1619
s = strchr (tmp , '@' );
1620
1620
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" );
1623
1624
return -1 ;
1624
1625
}
1625
1626
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
+
1626
1643
/* only a upsname is specified, fill in defaults */
1627
1644
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" );
1630
1647
return -1 ;
1631
1648
}
1632
1649
1633
1650
* port = PORT ;
1634
1651
return 0 ;
1635
1652
}
1636
1653
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
+
1637
1660
return upscli_splitaddr (s + 1 , hostname , port );
1638
1661
}
1639
1662
@@ -1659,8 +1682,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
1659
1682
return -1 ;
1660
1683
}
1661
1684
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" );
1664
1687
return -1 ;
1665
1688
}
1666
1689
@@ -1672,8 +1695,8 @@ int upscli_splitaddr(const char *buf, char **hostname, uint16_t *port)
1672
1695
} else {
1673
1696
s = strchr (tmp , ':' );
1674
1697
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" );
1677
1700
return -1 ;
1678
1701
}
1679
1702
0 commit comments