merge: branch 'bg/covscan'

Some coverity fixes.

https://github.com/NetworkManager/NetworkManager/pull/220
(cherry picked from commit 0cb5c4f611)
This commit is contained in:
Beniamino Galvani 2018-10-07 14:15:39 +02:00
commit faeda5fdbf
9 changed files with 49 additions and 22 deletions

View file

@ -45,7 +45,7 @@ handle_ip_file() {
} }
if [ "$2" != "pre-up" -a "$2" != "down" ]; then if [ "$2" != "pre-up" ] && [ "$2" != "down" ]; then
exit 0 exit 0
fi fi
@ -59,7 +59,7 @@ if [ -z "$profile" ]; then
exit 0 exit 0
fi fi
if ! [ -f "$dir/rule-$profile" -o -f "$dir/rule6-$profile" ]; then if [ ! -f "$dir/rule-$profile" ] && [ ! -f "$dir/rule6-$profile" ]; then
exit 0 exit 0
fi fi

View file

@ -1310,7 +1310,7 @@ nm_ip_route_attribute_validate (const char *name,
return FALSE; return FALSE;
} }
if (spec->type == G_VARIANT_TYPE_STRING) { if (g_variant_type_equal (spec->type, G_VARIANT_TYPE_STRING)) {
const char *string = g_variant_get_string (value, NULL); const char *string = g_variant_get_string (value, NULL);
gs_free char *string_free = NULL; gs_free char *string_free = NULL;
char *sep; char *sep;

View file

@ -433,7 +433,7 @@ nm_sriov_vf_attribute_validate (const char *name,
return FALSE; return FALSE;
} }
if (spec->type == G_VARIANT_TYPE_STRING) { if (g_variant_type_equal (spec->type, G_VARIANT_TYPE_STRING)) {
const char *string; const char *string;
switch (spec->str_type) { switch (spec->str_type) {

View file

@ -125,9 +125,9 @@ nm_team_link_watcher_new_ethtool (int delay_up,
NMTeamLinkWatcher *watcher; NMTeamLinkWatcher *watcher;
const char *val_fail = NULL; const char *val_fail = NULL;
if (delay_up < 0 || delay_up > G_MAXINT32) if (delay_up < 0 || !_NM_INT_LE_MAXINT32 (delay_up))
val_fail = "delay-up"; val_fail = "delay-up";
if (delay_down < 0 || delay_down > G_MAXINT32) if (delay_down < 0 || !_NM_INT_LE_MAXINT32 (delay_up))
val_fail = "delay-down"; val_fail = "delay-down";
if (val_fail) { if (val_fail) {
g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED, g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED,

View file

@ -1422,7 +1422,7 @@ nm_setting_diff (NMSetting *a,
} else { } else {
g_hash_table_iter_init (&iter, a_gendata->hash); g_hash_table_iter_init (&iter, a_gendata->hash);
while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) { while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) {
val2 = b_gendata ? g_hash_table_lookup (b_gendata->hash, key) : NULL; val2 = g_hash_table_lookup (b_gendata->hash, key);
compared_any = TRUE; compared_any = TRUE;
if ( !val2 if ( !val2
|| !g_variant_equal (val, val2)) { || !g_variant_equal (val, val2)) {
@ -1432,7 +1432,7 @@ nm_setting_diff (NMSetting *a,
} }
g_hash_table_iter_init (&iter, b_gendata->hash); g_hash_table_iter_init (&iter, b_gendata->hash);
while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) { while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) {
val2 = a_gendata ? g_hash_table_lookup (a_gendata->hash, key) : NULL; val2 = g_hash_table_lookup (a_gendata->hash, key);
compared_any = TRUE; compared_any = TRUE;
if ( !val2 if ( !val2
|| !g_variant_equal (val, val2)) { || !g_variant_equal (val, val2)) {

View file

@ -4941,18 +4941,18 @@ const char **nm_utils_enum_get_values (GType type, int from, int to)
static gboolean static gboolean
_nm_utils_is_json_object_no_validation (const char *str, GError **error) _nm_utils_is_json_object_no_validation (const char *str, GError **error)
{ {
if (str) { nm_assert (str);
/* libjansson also requires only utf-8 encoding. */
if (!g_utf8_validate (str, -1, NULL)) { /* libjansson also requires only utf-8 encoding. */
g_set_error_literal (error, if (!g_utf8_validate (str, -1, NULL)) {
NM_CONNECTION_ERROR, g_set_error_literal (error,
NM_CONNECTION_ERROR_INVALID_PROPERTY, NM_CONNECTION_ERROR,
_("not valid utf-8")); NM_CONNECTION_ERROR_INVALID_PROPERTY,
return FALSE; _("not valid utf-8"));
} return FALSE;
while (g_ascii_isspace (str[0]))
str++;
} }
while (g_ascii_isspace (str[0]))
str++;
/* do some very basic validation to see if this might be a JSON object. */ /* do some very basic validation to see if this might be a JSON object. */
if (str[0] == '{') { if (str[0] == '{') {

View file

@ -1236,7 +1236,7 @@ nm_utils_buf_utf8safe_unescape (const char *str, gsize *out_len, gpointer *to_fr
ch = (++str)[0]; ch = (++str)[0];
if (ch >= '0' && ch <= '7') { if (ch >= '0' && ch <= '7') {
v = v * 8 + (ch - '0'); v = v * 8 + (ch - '0');
ch = (++str)[0]; ++str;
} }
} }
ch = v; ch = v;

View file

@ -34,7 +34,7 @@ _NM_INT_NOT_NEGATIVE (gssize val)
* *
* When using such an enum for accessing an array, one naturally wants to check * When using such an enum for accessing an array, one naturally wants to check
* that the enum is not negative. However, the compiler doesn't like a plain * that the enum is not negative. However, the compiler doesn't like a plain
* comparisong "enum_val >= 0", because (if the enum is unsigned), it will warn * comparison "enum_val >= 0", because (if the enum is unsigned), it will warn
* that the expression is always true *duh*. Not even a cast to a signed * that the expression is always true *duh*. Not even a cast to a signed
* type helps to avoid the compiler warning in any case. * type helps to avoid the compiler warning in any case.
* *
@ -43,6 +43,33 @@ _NM_INT_NOT_NEGATIVE (gssize val)
return val >= 0; return val >= 0;
} }
/* check whether the integer value is smaller than G_MAXINT32. This macro exists
* for the sole purpose, that a plain "((int) value <= G_MAXINT32)" comparison
* may cause the compiler or coverity that this check is always TRUE. But the
* check depends on compile time and the size of C type "int". Of course, most
* of the time in is gint32 and an int value is always <= G_MAXINT32. The check
* exists to catch cases where that is not true.
*
* Together with the G_STATIC_ASSERT(), we make sure that this is always satisfied. */
G_STATIC_ASSERT (sizeof (int) == sizeof (gint32));
#if _NM_CC_SUPPORT_GENERIC
#define _NM_INT_LE_MAXINT32(value) \
({ \
_nm_unused typeof (value) _value = (value); \
\
_Generic((value), \
int: TRUE \
); \
})
#else
#define _NM_INT_LE_MAXINT32(value) ({ \
_nm_unused typeof (value) _value = (value); \
_nm_unused const int *_p_value = &_value; \
\
TRUE; \
})
#endif
/*****************************************************************************/ /*****************************************************************************/
static inline char static inline char

View file

@ -29,7 +29,7 @@ nm_device_ethernet_utils_get_default_wired_name (GHashTable *existing_ids)
int i; int i;
/* Find the next available unique connection name */ /* Find the next available unique connection name */
for (i = 1; i <= G_MAXINT; i++) { for (i = 1; i < G_MAXINT; i++) {
temp = g_strdup_printf (_("Wired connection %d"), i); temp = g_strdup_printf (_("Wired connection %d"), i);
if ( !existing_ids if ( !existing_ids
|| !g_hash_table_contains (existing_ids, temp)) || !g_hash_table_contains (existing_ids, temp))