From b447c80ad811850b9540225a96cb5496f8210b34 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 22:13:06 +0200 Subject: [PATCH 01/15] acd: avoid alloca() inside an unbounded loop --- src/devices/nm-acd-manager.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/devices/nm-acd-manager.c b/src/devices/nm-acd-manager.c index 735dd25b09..d9caadc6b6 100644 --- a/src/devices/nm-acd-manager.c +++ b/src/devices/nm-acd-manager.c @@ -81,7 +81,20 @@ _acd_event_to_string (unsigned int event) return NULL; } -#define acd_event_to_string_a(event) NM_UTILS_LOOKUP_STR_A (_acd_event_to_string, event) +#define ACD_EVENT_TO_STRING_BUF_SIZE 50 + +static const char * +_acd_event_to_string_buf (unsigned event, char buffer[ACD_EVENT_TO_STRING_BUF_SIZE]) +{ + const char *s; + + s = _acd_event_to_string (event); + if (s) + return s; + + g_snprintf (buffer, ACD_EVENT_TO_STRING_BUF_SIZE, "(%u)", event); + return buffer; +} static const char * acd_error_to_string (int error) @@ -172,6 +185,7 @@ acd_event (int fd, while ( !n_acd_pop_event (self->acd, &event) && event) { + char to_string_buffer[ACD_EVENT_TO_STRING_BUF_SIZE]; gs_free char *hwaddr_str = NULL; gboolean check_probing_done = FALSE; @@ -215,7 +229,7 @@ acd_event (int fd, nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex)); break; default: - _LOGD ("unhandled event '%s'", acd_event_to_string_a (event->event)); + _LOGD ("unhandled event '%s'", _acd_event_to_string_buf (event->event, to_string_buffer)); break; } From 53b229770199e197d6482a96710650a55609afe1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 22:29:46 +0200 Subject: [PATCH 02/15] ndisc: avoid static analysis complaining about overflow check in receive_ra() lgtm.com flags this. The check was there to be better safe than sorry. Also, it seems better to have code that shows what happens instead of a verbose code comment (or no comment at all). Anyway, avoid the false positive. --- src/ndisc/nm-lndp-ndisc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ndisc/nm-lndp-ndisc.c b/src/ndisc/nm-lndp-ndisc.c index b698489c0d..cff3db827b 100644 --- a/src/ndisc/nm-lndp-ndisc.c +++ b/src/ndisc/nm-lndp-ndisc.c @@ -179,9 +179,9 @@ receive_ra (struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) */ #define NM_NDISC_VLTIME_MULT ((guint32) 48) clamp_pltime = ndp_msgra_router_lifetime (msgra); - clamp_vltime = (clamp_pltime < G_MAXUINT32 / NM_NDISC_VLTIME_MULT) - ? clamp_pltime * NM_NDISC_VLTIME_MULT - : G_MAXUINT32; + + /* clamp_pltime has at most 16 bit set, and multiplication cannot overflow. */ + clamp_vltime = clamp_pltime * NM_NDISC_VLTIME_MULT; ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_PREFIX) { guint8 r_plen; From 6f0dadabd750e28a3617d798f1d9325d0f0c4b79 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 22:33:52 +0200 Subject: [PATCH 03/15] cli: avoid non-thread-safe localtime() function in nmcli Static analysis tools flag the use of localtime() because it is not thread safe. Of course, that was no problem here, but avoiding the warning is simple. Also, if we allocate 128 bytes, let strftime use it. --- clients/cli/connections.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 0e4a95e68a..ec0b9200a7 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -541,6 +541,8 @@ _metagen_con_show_get_fcn (NMC_META_GENERIC_INFO_GET_FCN_ARGS) if (info->info_type == NMC_GENERIC_INFO_TYPE_CON_SHOW_TIMESTAMP) return (*out_to_free = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp)); else { + struct tm localtime_result; + if (!timestamp) { if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) return _("never"); @@ -548,7 +550,7 @@ _metagen_con_show_get_fcn (NMC_META_GENERIC_INFO_GET_FCN_ARGS) } timestamp_real = timestamp; s_mut = g_malloc0 (128); - strftime (s_mut, 64, "%c", localtime (×tamp_real)); + strftime (s_mut, 127, "%c", localtime_r (×tamp_real, &localtime_result)); return (*out_to_free = s_mut); } } From b93e12cb43b6401ac3a891fe8841f08eddbf6b1d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 22:41:44 +0200 Subject: [PATCH 04/15] cli: avoid redundant "if" check that is always TRUE in nmcli_editor_tab_completion() --- clients/cli/connections.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index ec0b9200a7..65cc1e905e 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -6282,7 +6282,7 @@ nmcli_editor_tab_completion (const char *text, int start, int end) rl_completion_append_character = '.'; } else generator_func = gen_property_names; - } else if (num >= 3) { + } else { if (num == 3 && should_complete_files (NULL, line)) rl_attempted_completion_over = 0; else if (should_complete_vpn_uuids (NULL, line)) { From 3a1273f777133d5be745950dce4555c2adb664f0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:18:54 +0200 Subject: [PATCH 05/15] cli: avoid empty if block without a comment lgtm.com flags this as "Empty block without comment". Avoid it. This code is of course ugly. Much work was already done to port such occurrences, and more is needed. I won't add a FIXME comment, because lgtm.com flags those too. :) --- clients/cli/connections.c | 3 +++ clients/cli/devices.c | 4 ++++ clients/cli/general.c | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 65cc1e905e..e6d6c69905 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -1451,7 +1451,9 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) gboolean was_output = FALSE; if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) { + /* pass */ } else if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; @@ -2076,6 +2078,7 @@ do_connections_show (const NMCCommand *cmd, NmCli *nmc, int argc, const char *co if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) fields_str = NMC_FIELDS_CON_SHOW_COMMON; else if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 7031b43756..2d79d2ae9a 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -1436,6 +1436,7 @@ show_device_info (NMDevice *device, NmCli *nmc) if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) fields_str = NMC_FIELDS_DEV_SHOW_SECTIONS_COMMON; else if (g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; @@ -1749,6 +1750,7 @@ do_devices_status (const NMCCommand *cmd, NmCli *nmc, int argc, const char *cons if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) fields_str = "DEVICE,TYPE,STATE,CONNECTION"; else if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; @@ -3149,6 +3151,7 @@ do_device_wifi_list (const NMCCommand *cmd, NmCli *nmc, int argc, const char *co if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) fields_str = NMC_FIELDS_DEV_WIFI_LIST_COMMON; else if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; @@ -4651,6 +4654,7 @@ do_device_lldp_list (const NMCCommand *cmd, NmCli *nmc, int argc, const char *co if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) fields_str = NMC_FIELDS_DEV_LLDP_LIST_COMMON; else if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; diff --git a/clients/cli/general.c b/clients/cli/general.c index 747203bb06..05ff4dee37 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -545,7 +545,9 @@ print_permissions (void *user_data) } if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) { + /* pass */ } else if (g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; @@ -694,7 +696,9 @@ show_general_logging (NmCli *nmc) }; if (!nmc->required_fields || g_ascii_strcasecmp (nmc->required_fields, "common") == 0) { + /* pass */ } else if (g_ascii_strcasecmp (nmc->required_fields, "all") == 0) { + /* pass */ } else fields_str = nmc->required_fields; From 9acf32a7a8035ddee027baa4fef445c7e74696e2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:03:10 +0200 Subject: [PATCH 06/15] config: avoid lgtm.com warning about int bitfield for NMConfigDeviceStateData.nm_owned lgtm.com warns: int nm_owned:3; >> Bit field nm_owned of type int should have explicitly unsigned integral, explicitly signed integral, or enumeration type. So make it a NMTernary instead. It's nicer anyway. --- src/nm-config.c | 16 +++++++++------- src/nm-config.h | 4 ++-- src/nm-manager.c | 6 ++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/nm-config.c b/src/nm-config.c index 63347db7bb..9a16aa0374 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -2210,7 +2210,7 @@ _config_device_state_data_new (int ifindex, GKeyFile *kf) gs_free char *perm_hw_addr_fake = NULL; gsize connection_uuid_len; gsize perm_hw_addr_fake_len; - int nm_owned = -1; + NMTernary nm_owned; char *p; guint32 route_metric_default_effective; guint32 route_metric_default_aspired; @@ -2252,7 +2252,7 @@ _config_device_state_data_new (int ifindex, GKeyFile *kf) nm_owned = nm_config_keyfile_get_boolean (kf, DEVICE_RUN_STATE_KEYFILE_GROUP_DEVICE, DEVICE_RUN_STATE_KEYFILE_KEY_DEVICE_NM_OWNED, - -1); + NM_TERNARY_DEFAULT); /* metric zero is not a valid metric. While zero valid for IPv4, for IPv6 it is an alias * for 1024. Since we handle here IPv4 and IPv6 the same, we cannot allow zero. */ @@ -2325,9 +2325,11 @@ nm_config_device_state_load (int ifindex) return NULL; device_state = _config_device_state_data_new (ifindex, kf); - nm_owned_str = device_state->nm_owned == TRUE ? - ", nm-owned=1" : - (device_state->nm_owned == FALSE ? ", nm-owned=0" : ""); + nm_owned_str = device_state->nm_owned == NM_TERNARY_TRUE + ? ", nm-owned=1" + : ( device_state->nm_owned == NM_TERNARY_FALSE + ? ", nm-owned=0" + : ""); _LOGT ("device-state: %s #%d (%s); managed=%s%s%s%s%s%s%s%s, route-metric-default=%"G_GUINT32_FORMAT"-%"G_GUINT32_FORMAT"", kf ? "read" : "miss", @@ -2390,7 +2392,7 @@ nm_config_device_state_write (int ifindex, NMConfigDeviceStateManagedType managed, const char *perm_hw_addr_fake, const char *connection_uuid, - int nm_owned, + NMTernary nm_owned, guint32 route_metric_default_aspired, guint32 route_metric_default_effective, const char *next_server, @@ -2429,7 +2431,7 @@ nm_config_device_state_write (int ifindex, DEVICE_RUN_STATE_KEYFILE_KEY_DEVICE_CONNECTION_UUID, connection_uuid); } - if (nm_owned >= 0) { + if (nm_owned != NM_TERNARY_DEFAULT) { g_key_file_set_boolean (kf, DEVICE_RUN_STATE_KEYFILE_GROUP_DEVICE, DEVICE_RUN_STATE_KEYFILE_KEY_DEVICE_NM_OWNED, diff --git a/src/nm-config.h b/src/nm-config.h index b4478ceb04..0fce7ec0cb 100644 --- a/src/nm-config.h +++ b/src/nm-config.h @@ -243,7 +243,7 @@ struct _NMConfigDeviceStateData { /* whether the device was nm-owned (0/1) or -1 for * non-software devices. */ - int nm_owned:3; + NMTernary nm_owned:3; }; NMConfigDeviceStateData *nm_config_device_state_load (int ifindex); @@ -252,7 +252,7 @@ gboolean nm_config_device_state_write (int ifindex, NMConfigDeviceStateManagedType managed, const char *perm_hw_addr_fake, const char *connection_uuid, - int nm_owned, + NMTernary nm_owned, guint32 route_metric_default_aspired, guint32 route_metric_default_effective, const char *next_server, diff --git a/src/nm-manager.c b/src/nm-manager.c index bbe501259f..8cee206bef 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -6552,7 +6552,7 @@ nm_manager_write_device_state (NMManager *self, NMDevice *device, int *out_ifind gboolean perm_hw_addr_is_fake; guint32 route_metric_default_aspired; guint32 route_metric_default_effective; - int nm_owned; + NMTernary nm_owned; NMDhcpConfig *dhcp_config; const char *next_server = NULL; const char *root_path = NULL; @@ -6588,7 +6588,9 @@ nm_manager_write_device_state (NMManager *self, NMDevice *device, int *out_ifind if (perm_hw_addr_fake && !perm_hw_addr_is_fake) perm_hw_addr_fake = NULL; - nm_owned = nm_device_is_software (device) ? nm_device_is_nm_owned (device) : -1; + nm_owned = nm_device_is_software (device) + ? nm_device_is_nm_owned (device) + : NM_TERNARY_DEFAULT; route_metric_default_effective = _device_route_metric_get (self, ifindex, NM_DEVICE_TYPE_UNKNOWN, TRUE, &route_metric_default_aspired); From 9e6d6191d1d76c71edd02a8781a5049b4041707c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:06:36 +0200 Subject: [PATCH 07/15] tests: add include guard to "nm-test-libnm-utils.h" header It causes a warning on lgtm.com. --- shared/nm-test-libnm-utils.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shared/nm-test-libnm-utils.h b/shared/nm-test-libnm-utils.h index 06dbc72d71..04e8fb649b 100644 --- a/shared/nm-test-libnm-utils.h +++ b/shared/nm-test-libnm-utils.h @@ -3,6 +3,9 @@ * Copyright (C) 2014 - 2015 Red Hat, Inc. */ +#ifndef __NM_TEST_LIBNM_UTILS_H__ +#define __NM_TEST_LIBNM_UTILS_H__ + #include "NetworkManager.h" #include "nm-utils/nm-test-utils.h" @@ -82,3 +85,5 @@ nmtstc_client_new (gboolean allow_iterate_main_context) { return nmtstc_context_object_new (NM_TYPE_CLIENT, allow_iterate_main_context, NULL); } + +#endif /* __NM_TEST_LIBNM_UTILS_H__ */ From 0258616bd8ea65be0bff4fac94880cba12a0b94a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 22:37:07 +0200 Subject: [PATCH 08/15] libnm: fix redundant line constructing dbus_type_name_map in "generate-setting-docs.py" --- libnm/generate-setting-docs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/libnm/generate-setting-docs.py b/libnm/generate-setting-docs.py index 025689ea3c..2bd13f5aa4 100755 --- a/libnm/generate-setting-docs.py +++ b/libnm/generate-setting-docs.py @@ -47,7 +47,6 @@ dbus_type_name_map = { 'aay': 'array of byte array', 'a(ayuay)': 'array of legacy IPv6 address struct', 'a(ayuayu)': 'array of legacy IPv6 route struct', - 'aa{sv}': 'array of vardict', } ns_map = { From cc1c2c9fc96eb9eeae5ddcb29c350a062e6781e7 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:16:34 +0200 Subject: [PATCH 09/15] libnm: remove unused import from "generate-setting-docs.py" --- libnm/generate-setting-docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnm/generate-setting-docs.py b/libnm/generate-setting-docs.py index 2bd13f5aa4..38b914b3c9 100755 --- a/libnm/generate-setting-docs.py +++ b/libnm/generate-setting-docs.py @@ -10,7 +10,7 @@ import os import gi gi.require_version('GIRepository', '2.0') from gi.repository import GIRepository -import argparse, datetime, re, sys +import argparse, re, sys import xml.etree.ElementTree as ET try: From a5614dc469aada7c3b2b294a318defb40a50205b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:09:21 +0200 Subject: [PATCH 10/15] examples: remove unused assignment in "examples/python/gi/wifi-p2p.py" lgtm.com warns about this. --- examples/python/gi/wifi-p2p.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/python/gi/wifi-p2p.py b/examples/python/gi/wifi-p2p.py index 746d8d7975..804df34c89 100755 --- a/examples/python/gi/wifi-p2p.py +++ b/examples/python/gi/wifi-p2p.py @@ -82,7 +82,7 @@ def scan_timeout_cb(device): def start_find_cb(device, async_result, user_data): try: - r = device.start_find_finish(async_result) + device.start_find_finish(async_result) except Exception as e: sys.stderr.write("Error: %s\n" % e) main_loop.quit() From 41a7a262591e25985431a70e8b597ce85d10ebd6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:16:34 +0200 Subject: [PATCH 11/15] examples: remove unused imports from python examples --- examples/python/dbus/get-active-connection-uuids.py | 2 +- examples/python/dbus/list-devices.py | 2 +- examples/python/dbus/wifi-active-ap.py | 2 +- examples/python/gi/checkpoint.py | 2 -- examples/python/gi/dns.py | 1 - examples/python/gi/get-active-connections.py | 2 +- examples/python/gi/get-devices.py | 2 +- examples/python/gi/get-interface-flags.py | 2 +- examples/python/gi/get_ips.py | 2 +- examples/python/gi/nm-add-connection2.py | 1 - examples/python/gi/nm-update2.py | 1 - examples/python/gi/nm-wg-set | 1 - 12 files changed, 7 insertions(+), 13 deletions(-) diff --git a/examples/python/dbus/get-active-connection-uuids.py b/examples/python/dbus/get-active-connection-uuids.py index 8d0e5c15e7..dd168c0aab 100755 --- a/examples/python/dbus/get-active-connection-uuids.py +++ b/examples/python/dbus/get-active-connection-uuids.py @@ -4,7 +4,7 @@ # Copyright (C) 2010 Red Hat, Inc. # -import dbus, sys +import dbus # This example takes a device interface name as a parameter and tells # NetworkManager to disconnect that device, closing down any network diff --git a/examples/python/dbus/list-devices.py b/examples/python/dbus/list-devices.py index 4a9ebe4e53..aebc902bbe 100755 --- a/examples/python/dbus/list-devices.py +++ b/examples/python/dbus/list-devices.py @@ -4,7 +4,7 @@ # Copyright (C) 2011 - 2012 Red Hat, Inc. # -import dbus, sys +import dbus # This example lists basic information about network interfaces known to NM diff --git a/examples/python/dbus/wifi-active-ap.py b/examples/python/dbus/wifi-active-ap.py index 790f11a9ef..b81c06a933 100755 --- a/examples/python/dbus/wifi-active-ap.py +++ b/examples/python/dbus/wifi-active-ap.py @@ -11,7 +11,7 @@ # https://developer.gnome.org/NetworkManager/1.0/ref-settings.html # -import dbus, sys, time +import dbus, sys bus = dbus.SystemBus() service_name = "org.freedesktop.NetworkManager" diff --git a/examples/python/gi/checkpoint.py b/examples/python/gi/checkpoint.py index b3b4e0f8a5..b1ecbb8984 100755 --- a/examples/python/gi/checkpoint.py +++ b/examples/python/gi/checkpoint.py @@ -10,8 +10,6 @@ import gi gi.require_version('NM', '1.0') from gi.repository import GLib, NM -import os - ############################################################################### def usage(): diff --git a/examples/python/gi/dns.py b/examples/python/gi/dns.py index f93f97e514..da2a6a6c43 100755 --- a/examples/python/gi/dns.py +++ b/examples/python/gi/dns.py @@ -4,7 +4,6 @@ # Copyright (C) 2016 Red Hat, Inc. # -import sys import gi gi.require_version('NM', '1.0') from gi.repository import GLib, NM diff --git a/examples/python/gi/get-active-connections.py b/examples/python/gi/get-active-connections.py index 072f245ab0..6da8b61890 100755 --- a/examples/python/gi/get-active-connections.py +++ b/examples/python/gi/get-active-connections.py @@ -8,7 +8,7 @@ import gi gi.require_version('NM', '1.0') -from gi.repository import GLib, NM +from gi.repository import NM if __name__ == "__main__": client = NM.Client.new(None) diff --git a/examples/python/gi/get-devices.py b/examples/python/gi/get-devices.py index 113d384b30..88bcc8945b 100755 --- a/examples/python/gi/get-devices.py +++ b/examples/python/gi/get-devices.py @@ -8,7 +8,7 @@ import gi gi.require_version('NM', '1.0') -from gi.repository import GLib, NM +from gi.repository import NM if __name__ == "__main__": client = NM.Client.new(None) diff --git a/examples/python/gi/get-interface-flags.py b/examples/python/gi/get-interface-flags.py index 756999169b..e1d1721a4f 100755 --- a/examples/python/gi/get-interface-flags.py +++ b/examples/python/gi/get-interface-flags.py @@ -6,7 +6,7 @@ import gi gi.require_version('NM', '1.0') -from gi.repository import GLib, NM +from gi.repository import NM if __name__ == "__main__": client = NM.Client.new(None) diff --git a/examples/python/gi/get_ips.py b/examples/python/gi/get_ips.py index 13b0a8b6db..75be2f8c5d 100755 --- a/examples/python/gi/get_ips.py +++ b/examples/python/gi/get_ips.py @@ -7,7 +7,7 @@ import sys, socket import gi gi.require_version('NM', '1.0') -from gi.repository import GLib, NM +from gi.repository import NM # # This example shows how to get addresses, routes and DNS information diff --git a/examples/python/gi/nm-add-connection2.py b/examples/python/gi/nm-add-connection2.py index 01c1ae7bd9..c5ec3f3bbe 100755 --- a/examples/python/gi/nm-add-connection2.py +++ b/examples/python/gi/nm-add-connection2.py @@ -5,7 +5,6 @@ # import sys -import re import gi gi.require_version('NM', '1.0') diff --git a/examples/python/gi/nm-update2.py b/examples/python/gi/nm-update2.py index 5a65879371..23e277c78d 100755 --- a/examples/python/gi/nm-update2.py +++ b/examples/python/gi/nm-update2.py @@ -5,7 +5,6 @@ # import sys -import re import gi gi.require_version('NM', '1.0') diff --git a/examples/python/gi/nm-wg-set b/examples/python/gi/nm-wg-set index ca31ac7207..81d86c7c0a 100755 --- a/examples/python/gi/nm-wg-set +++ b/examples/python/gi/nm-wg-set @@ -58,7 +58,6 @@ # nm-wg-set id "$PROFILE" $WG_ARGS import sys -import re import os import gi From 5c08981356a1038f49050d748f09d859bf471810 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 22:33:52 +0200 Subject: [PATCH 12/15] examples: avoid non-thread-safe localtime() function example Just to silence the warning on lgtm.com. --- examples/C/glib/list-connections-libnm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/C/glib/list-connections-libnm.c b/examples/C/glib/list-connections-libnm.c index 77d5dfb729..7bed0c014e 100644 --- a/examples/C/glib/list-connections-libnm.c +++ b/examples/C/glib/list-connections-libnm.c @@ -30,10 +30,15 @@ show_connection (NMConnection *connection) s_con = nm_connection_get_setting_connection (connection); if (s_con) { + struct tm localtime_data; + /* Get various info from NMSettingConnection and show it */ timestamp = nm_setting_connection_get_timestamp (s_con); timestamp_str = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp); - strftime (timestamp_real_str, sizeof (timestamp_real_str), "%c", localtime ((time_t *) ×tamp)); + strftime (timestamp_real_str, + sizeof (timestamp_real_str), + "%c", + localtime_r ((time_t *) ×tamp, &localtime_data)); val1 = nm_setting_connection_get_id (s_con); val2 = nm_setting_connection_get_uuid (s_con); From c7d0a8605067a7b86a36bc2ce5470d0367a4d737 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:40:43 +0200 Subject: [PATCH 13/15] examples: avoid "except" for BaseException in examples lgtm.com flags this as "Except block directly handles BaseException". --- examples/python/gi/checkpoint.py | 2 +- examples/python/gi/nm-wg-set | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/python/gi/checkpoint.py b/examples/python/gi/checkpoint.py index b1ecbb8984..a695a64613 100755 --- a/examples/python/gi/checkpoint.py +++ b/examples/python/gi/checkpoint.py @@ -134,7 +134,7 @@ def do_adjust_rollback_timeout(client): sys.exit("Missing timeout") try: add_timeout = int(sys.argv[3]) - except: + except Exception: sys.exit("Invalid timeout") path = validate_path(sys.argv[2], client) diff --git a/examples/python/gi/nm-wg-set b/examples/python/gi/nm-wg-set index 81d86c7c0a..1f5279ee69 100755 --- a/examples/python/gi/nm-wg-set +++ b/examples/python/gi/nm-wg-set @@ -442,7 +442,7 @@ if __name__ == '__main__': secrets = conn.get_secrets(NM.SETTING_WIREGUARD_SETTING_NAME) if secrets: conn.update_secrets(NM.SETTING_WIREGUARD_SETTING_NAME, secrets) - except: + except Exception: pass if not argv: From 8ad448444a14cbb421e89509da9c2a5d30eb4004 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:42:13 +0200 Subject: [PATCH 14/15] examples: avoid "x == None" checks in python examples lgmt.com says "Testing for None should use the is operator". --- examples/python/dbus/checkpoint.py | 2 +- examples/python/gi/deactivate-all.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/python/dbus/checkpoint.py b/examples/python/dbus/checkpoint.py index 62c7573b8c..1ea1b1a7fc 100755 --- a/examples/python/dbus/checkpoint.py +++ b/examples/python/dbus/checkpoint.py @@ -41,7 +41,7 @@ devList = [] for arg in sys.argv[2:]: path = GetDevicePath(arg) - if path == None: + if path is None: raise Exception("NetworkManager knows nothing about %s" % arg) else: devList.append(path) diff --git a/examples/python/gi/deactivate-all.py b/examples/python/gi/deactivate-all.py index 3af350e4c3..7e528d0b3e 100755 --- a/examples/python/gi/deactivate-all.py +++ b/examples/python/gi/deactivate-all.py @@ -57,7 +57,7 @@ if __name__ == "__main__": # deactivate the connections for ac in connections: - if ctype == None or ctype == ac.get_connection_type(): + if ctype is None or ctype == ac.get_connection_type(): sys.stdout.write("Deactivating %s (%s)" % (ac.get_id(), ac.get_uuid())) try: client.deactivate_connection(ac, None) From 64331d6085c39ac563fa64e2cf2b1a8a17dec38e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 May 2020 23:43:57 +0200 Subject: [PATCH 15/15] examples: remove unused code from "examples/python/dbus/vpn.py" lgtm.com says "The value assigned to local variable all_connections is never used". Just drop the entire statement. It's not right there. --- examples/python/dbus/vpn.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/python/dbus/vpn.py b/examples/python/dbus/vpn.py index 8939d26206..14fa2f1d2e 100755 --- a/examples/python/dbus/vpn.py +++ b/examples/python/dbus/vpn.py @@ -56,7 +56,6 @@ def get_active_connection_path(uuid): proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager') iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties') active_connections = iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections') - all_connections = get_connections() for a in active_connections: proxy = bus.get_object('org.freedesktop.NetworkManager', a)