all: replace uses of inet_aton() and friends

rpmdiff complains about uses of inet_aton, inet_makeaddr, inet_netof,
inet_ntoa under the IPv6 section:

   usr/sbin/NetworkManager on aarch64 i686 x86_64 ppc ppc64 ppc64le s390 s390x uses function inet_aton, which may impact IPv6 support

I think the warning is bogus, but refactor our code to avoid it.

Note that systemd code still uses them, so it don't avoid the rpmdiff
warning. But let's not diverge our systemd import from upstream for this.

- for NMSettingBond:validate_ip() also avoid g_strsplit_set() which
  allocates a full strv. Instead, we can do with one g_strdup().

- for test-resolvconf-capture.c, replace the functions with macros.
  Macros should be avoided usually, but for test asserts they are
  more convenient as they preserved the __FILE__:__LINE__ of where
  the assertion fails.
This commit is contained in:
Thomas Haller 2017-06-02 13:15:18 +02:00
parent c0419257e7
commit ea6648cea1
4 changed files with 57 additions and 45 deletions

View file

@ -224,19 +224,34 @@ validate_list (const char *name, const char *value, const BondDefault *def)
static gboolean
validate_ip (const char *name, const char *value)
{
char **ips, **iter;
gboolean success = TRUE;
gs_free char *value_clone = NULL;
struct in_addr addr;
if (!value || !value[0])
return FALSE;
ips = g_strsplit_set (value, ",", 0);
for (iter = ips; iter && *iter && success; iter++)
success = !!inet_aton (*iter, &addr);
g_strfreev (ips);
value_clone = g_strdup (value);
value = value_clone;
for (;;) {
char *eow;
return success;
/* we do not skip over empty words. E.g
* "192.168.1.1," is an error.
*
* ... for no particular reason. */
eow = strchr (value, ',');
if (eow)
*eow = '\0';
if (inet_pton (AF_INET, value, &addr) != 1)
return FALSE;
if (!eow)
break;
value = eow + 1;
}
return TRUE;
}
static gboolean

View file

@ -258,19 +258,34 @@ validate_list (const char *name, const char *value, const BondDefault *def)
static gboolean
validate_ip (const char *name, const char *value)
{
char **ips, **iter;
gboolean success = TRUE;
gs_free char *value_clone = NULL;
struct in_addr addr;
if (!value || !value[0])
return FALSE;
ips = g_strsplit_set (value, ",", 0);
for (iter = ips; iter && *iter && success; iter++)
success = !!inet_aton (*iter, &addr);
g_strfreev (ips);
value_clone = g_strdup (value);
value = value_clone;
for (;;) {
char *eow;
return success;
/* we do not skip over empty words. E.g
* "192.168.1.1," is an error.
*
* ... for no particular reason. */
eow = strchr (value, ',');
if (eow)
*eow = '\0';
if (inet_pton (AF_INET, value, &addr) != 1)
return FALSE;
if (!eow)
break;
value = eow + 1;
}
return TRUE;
}
static gboolean

View file

@ -863,19 +863,19 @@ test_read_lease_ip4_config_basic (void)
/* Address */
g_assert_cmpint (nm_ip4_config_get_num_addresses (config), ==, 1);
g_assert (inet_aton ("192.168.1.180", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("192.168.1.180");
addr = nm_ip4_config_get_address (config, 0);
g_assert_cmpint (addr->address, ==, expected_addr);
g_assert_cmpint (addr->peer_address, ==, expected_addr);
g_assert_cmpint (addr->plen, ==, 24);
/* Gateway */
g_assert (inet_aton ("192.168.1.1", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("192.168.1.1");
g_assert_cmpint (nm_ip4_config_get_gateway (config), ==, expected_addr);
/* DNS */
g_assert_cmpint (nm_ip4_config_get_num_nameservers (config), ==, 1);
g_assert (inet_aton ("192.168.1.1", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("192.168.1.1");
g_assert_cmpint (nm_ip4_config_get_nameserver (config, 0), ==, expected_addr);
g_assert_cmpint (nm_ip4_config_get_num_domains (config), ==, 0);
@ -886,21 +886,21 @@ test_read_lease_ip4_config_basic (void)
/* Address */
g_assert_cmpint (nm_ip4_config_get_num_addresses (config), ==, 1);
g_assert (inet_aton ("10.77.52.141", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("10.77.52.141");
addr = nm_ip4_config_get_address (config, 0);
g_assert_cmpint (addr->address, ==, expected_addr);
g_assert_cmpint (addr->peer_address, ==, expected_addr);
g_assert_cmpint (addr->plen, ==, 8);
/* Gateway */
g_assert (inet_aton ("10.77.52.254", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("10.77.52.254");
g_assert_cmpint (nm_ip4_config_get_gateway (config), ==, expected_addr);
/* DNS */
g_assert_cmpint (nm_ip4_config_get_num_nameservers (config), ==, 2);
g_assert (inet_aton ("8.8.8.8", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("8.8.8.8");
g_assert_cmpint (nm_ip4_config_get_nameserver (config, 0), ==, expected_addr);
g_assert (inet_aton ("8.8.4.4", (struct in_addr *) &expected_addr));
expected_addr = nmtst_inet4_from_string ("8.8.4.4");
g_assert_cmpint (nm_ip4_config_get_nameserver (config, 1), ==, expected_addr);
/* Domains */

View file

@ -46,32 +46,14 @@ test_capture_empty (void)
g_array_free (ns6, TRUE);
}
static void
assert_dns4_entry (const GArray *a, guint i, const char *s)
{
guint32 n, m;
#define assert_dns4_entry(a, i, s) \
g_assert_cmpint ((g_array_index ((a), guint32, (i))), ==, nmtst_inet4_from_string (s));
g_assert (inet_aton (s, (void *) &n) != 0);
m = g_array_index (a, guint32, i);
g_assert_cmpint (m, ==, n);
}
#define assert_dns6_entry(a, i, s) \
g_assert (IN6_ARE_ADDR_EQUAL (&g_array_index ((a), struct in6_addr, (i)), nmtst_inet6_from_string (s)))
static void
assert_dns6_entry (const GArray *a, guint i, const char *s)
{
struct in6_addr n = IN6ADDR_ANY_INIT;
struct in6_addr *m;
g_assert (inet_pton (AF_INET6, s, (void *) &n) == 1);
m = &g_array_index (a, struct in6_addr, i);
g_assert (IN6_ARE_ADDR_EQUAL (&n, m));
}
static void
assert_dns_option (GPtrArray *a, guint i, const char *s)
{
g_assert_cmpstr (a->pdata[i], ==, s);
}
#define assert_dns_option(a, i, s) \
g_assert_cmpstr ((a)->pdata[(i)], ==, (s));
static void
test_capture_basic4 (void)