Browse Source

common/ipaddr: remove leading and trailing whitespaces

Leading or trailing whitespaces are not removed from the IP address,
for example, when specified by the user.  If the IP address contains
whitespaces, parsing the address fails and a malformed address is
reported even if the IP address would be valid.

To remove trailing whitespaces, extend the rtrim() function to remove
multiple spaces.  Introduce a new function, strtrim() to remove any
leading spaces.

Also add a test case to verify the changed behavior.

Signed-off-by: Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
tags/1.138
Hendrik Brueckner Dimitri John Ledkov 5 years ago
parent
commit
f4dfa4080f
No known key found for this signature in database GPG Key ID: CAC2D8B9CD2CA5F9
4 changed files with 56 additions and 3 deletions
  1. +8
    -0
      debian/changelog
  2. +21
    -3
      netcfg-common.c
  3. +1
    -0
      netcfg.h
  4. +26
    -0
      test/test_netcfg_parse_cidr_address.c

+ 8
- 0
debian/changelog View File

@@ -1,3 +1,11 @@
netcfg (1.138) UNRELEASED; urgency=medium

[ Hendrik Brueckner ]
* common/ipaddr: remove leading and trailing whitespaces
* Closes: #818611, LP: #1541955

-- Dimitri John Ledkov <xnox@ubuntu.com> Wed, 30 Mar 2016 12:13:56 +0100

netcfg (1.137) unstable; urgency=medium

[ Colin Watson ]


+ 21
- 3
netcfg-common.c View File

@@ -1577,10 +1577,11 @@ int netcfg_parse_cidr_address(const char *address, struct netcfg_interface *inte
struct in_addr addr;
struct in6_addr addr6;
int ok;
char *maskptr, addrstr[NETCFG_ADDRSTRLEN];
char *maskptr, *addrstr, addrbuf[NETCFG_ADDRSTRLEN];
int i;
strncpy(addrstr, address, NETCFG_ADDRSTRLEN);
strncpy(addrbuf, address, NETCFG_ADDRSTRLEN);
addrstr = strtrim(addrbuf);
if ((maskptr = strchr(addrstr, '/'))) {
/* Houston, we have a netmask; split it into bits */
@@ -1730,7 +1731,24 @@ void rtrim(char *s)
n = strlen(s) - 1;
while (isspace(s[n])) {
while (n >= 0 && isspace(s[n])) {
s[n] = '\0';
n--;
}
}

char *strtrim(char *s)
{
size_t len;

len = strlen(s);
if (!len)
return s;

rtrim(s);

while (*s && isspace(*s))
s++;

return s;
}

+ 1
- 0
netcfg.h View File

@@ -249,6 +249,7 @@ extern void preseed_hostname_from_fqdn(struct debconfclient *client, char *fqdn)
extern int netcfg_dhcp(struct debconfclient *client, struct netcfg_interface *interface);

extern void rtrim(char *);
extern char *strtrim(char *s);

/* ipv6.c */
extern void nc_v6_wait_for_complete_configuration(const struct netcfg_interface *interface);


+ 26
- 0
test/test_netcfg_parse_cidr_address.c View File

@@ -101,6 +101,31 @@ START_TEST(test_parse_cidr_v6_address)
}
END_TEST

START_TEST(test_parse_cidr_ignore_leading_trailing_spaces)
{
struct netcfg_interface interface;
netcfg_interface_init(&interface);
int rv;

interface.masklen = 7;
rv = netcfg_parse_cidr_address(" 192.0.2.12 ", &interface);

fail_unless (rv,
"parsing failed, rv = %i", rv);

fail_unless (interface.masklen == 0,
"masklen was %i, should have been 0",
interface.masklen);

fail_unless (strcmp("192.0.2.12", interface.ipaddress) == 0,
"IP address was %s, should have been 192.10.2.12",
interface.ipaddress);

fail_unless (interface.address_family == AF_INET,
"Address family should have been AF_INET");
}
END_TEST

Suite *test_netcfg_parse_cidr_address_suite (void)
{
Suite *s = suite_create ("netcfg_parse_cidr_address");
@@ -110,6 +135,7 @@ Suite *test_netcfg_parse_cidr_address_suite (void)
tcase_add_test (tc, test_parse_cidr_v4_address);
tcase_add_test (tc, test_parse_standalone_v6_address);
tcase_add_test (tc, test_parse_cidr_v6_address);
tcase_add_test (tc, test_parse_cidr_ignore_leading_trailing_spaces);
suite_add_tcase (s, tc);


Loading…
Cancel
Save