Skip to content

Commit fa53e33

Browse files
committed
Fix potential runtime errors and resource leaks
This commit addresses multiple potential runtime errors identified through static analysis.
1 parent 54bfa63 commit fa53e33

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

guiconfig.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ def menuconfig(kconf):
224224

225225
# Select the first item and focus the Treeview, so that keyboard controls
226226
# work immediately
227-
_select(_tree, _tree.get_children()[0])
227+
children = _tree.get_children()
228+
if children:
229+
_select(_tree, children[0])
228230
_tree.focus_set()
229231

230232
# Make geometry information available for centering the window. This
@@ -1249,15 +1251,18 @@ def _set_val(sc, val):
12491251
# rare cases, but is fast and flicker-free.
12501252

12511253
stayput = _loc_ref_item() # Item to preserve scroll for
1252-
old_row = _item_row(stayput)
1254+
if stayput:
1255+
old_row = _item_row(stayput)
12531256

1254-
_update_tree()
1257+
_update_tree()
12551258

1256-
# If the reference item disappeared (can happen if the change was done
1257-
# from the jump-to dialog), then avoid messing with the scroll and hope
1258-
# for the best
1259-
if _attached(stayput):
1260-
_tree.yview_scroll(_item_row(stayput) - old_row, "units")
1259+
# If the reference item disappeared (can happen if the change was done
1260+
# from the jump-to dialog), then avoid messing with the scroll and hope
1261+
# for the best
1262+
if _attached(stayput):
1263+
_tree.yview_scroll(_item_row(stayput) - old_row, "units")
1264+
else:
1265+
_update_tree()
12611266

12621267
if _jump_to_tree:
12631268
_update_jump_to_display()
@@ -1630,7 +1635,12 @@ def _toggle_tree_mode(_):
16301635
def _do_tree_mode():
16311636
# Updates the UI for the current tree mode (full-tree or single-menu)
16321637

1633-
loc_ref_node = _id_to_node[_loc_ref_item()]
1638+
loc_ref = _loc_ref_item()
1639+
if not loc_ref:
1640+
# Tree is empty (should not happen in normal use)
1641+
return
1642+
1643+
loc_ref_node = _id_to_node[loc_ref]
16341644

16351645
if not _single_menu:
16361646
# _jump_to() -> _enter_menu() already updates the tree, but
@@ -1647,7 +1657,9 @@ def _enter_menu_and_select_first(menu):
16471657
# mode.
16481658

16491659
_enter_menu(menu)
1650-
_select(_tree, _tree.get_children()[0])
1660+
children = _tree.get_children()
1661+
if children:
1662+
_select(_tree, children[0])
16511663

16521664

16531665
def _enter_menu(menu):
@@ -1700,7 +1712,11 @@ def _loc_ref_item():
17001712

17011713
# Otherwise, use the middle item on the screen. If it doesn't exist, the
17021714
# tree is probably really small, so use the first item in the entire tree.
1703-
return _tree.identify_row(_tree.winfo_height() // 2) or _tree.get_children()[0]
1715+
middle_item = _tree.identify_row(_tree.winfo_height() // 2)
1716+
if middle_item:
1717+
return middle_item
1718+
children = _tree.get_children()
1719+
return children[0] if children else None
17041720

17051721

17061722
def _vis_loc_ref_item():
@@ -1807,7 +1823,10 @@ def _try_save(save_fn, filename, description):
18071823
messagebox.showerror(
18081824
"Error saving " + description,
18091825
"Error saving {} to '{}': {} (errno: {})".format(
1810-
description, e.filename, e.strerror, errno.errorcode[e.errno]
1826+
description,
1827+
e.filename,
1828+
e.strerror,
1829+
errno.errorcode.get(e.errno, e.errno),
18111830
),
18121831
)
18131832
return False
@@ -1829,7 +1848,7 @@ def _try_load(filename):
18291848
messagebox.showerror(
18301849
"Error loading configuration",
18311850
"Error loading '{}': {} (errno: {})".format(
1832-
filename, e.strerror, errno.errorcode[e.errno]
1851+
filename, e.strerror, errno.errorcode.get(e.errno, e.errno)
18331852
),
18341853
)
18351854
return False

kconfiglib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6815,10 +6815,10 @@ def _touch_dep_file(path, sym_name):
68156815
def _save_old(path):
68166816
# See write_config()
68176817

6818-
def copy(src, dst):
6819-
# Import as needed, to save some startup time
6820-
import shutil
6818+
# Import as needed, to save some startup time
6819+
import shutil
68216820

6821+
def copy(src, dst):
68226822
shutil.copyfile(src, dst)
68236823

68246824
if islink(path):
@@ -6837,7 +6837,7 @@ def copy(src, dst):
68376837

68386838
try:
68396839
copy_fn(path, path + ".old")
6840-
except Exception:
6840+
except (OSError, IOError, shutil.Error):
68416841
# Ignore errors from 'path' missing as well as other errors.
68426842
# <filename>.old file is usually more of a nice-to-have, and not worth
68436843
# erroring out over e.g. if <filename>.old happens to be a directory or

setup.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
import setuptools
55

66

7+
# Make sure that README.md decodes on Python 3 in environments that use
8+
# the C locale (which implies ASCII), by explicitly giving the encoding.
9+
#
10+
# io.open() has the 'encoding' parameter on both Python 2 and 3. open()
11+
# doesn't have it on Python 2. This lets us use the same code for both.
12+
with io.open(
13+
os.path.join(os.path.dirname(__file__), "README.md"), encoding="utf-8"
14+
) as f:
15+
long_description = f.read()
16+
17+
718
setuptools.setup(
819
name="kconfiglib",
920
# MAJOR.MINOR.PATCH, per http://semver.org
1021
version="14.1.1a4",
1122
description="A flexible Python Kconfig implementation",
12-
# Make sure that README.md decodes on Python 3 in environments that use
13-
# the C locale (which implies ASCII), by explicitly giving the encoding.
14-
#
15-
# io.open() has the 'encoding' parameter on both Python 2 and 3. open()
16-
# doesn't have it on Python 2. This lets us use the same code for both.
17-
long_description=io.open(
18-
os.path.join(os.path.dirname(__file__), "README.md"), encoding="utf-8"
19-
).read(),
23+
long_description=long_description,
2024
url="https://github.com/sysprog21/Kconfiglib",
2125
author="Zephyr Project",
2226
author_email="[email protected]",

testsuite.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,15 +3645,13 @@ def equal_configs():
36453645
their = their[i:]
36463646

36473647
try:
3648-
f = open("._config")
3648+
with open("._config") as f:
3649+
our = f.readlines()
36493650
except EnvironmentError as e:
36503651
if e.errno != errno.ENOENT:
36513652
raise
36523653
print("._config not found. Did you forget to apply the Makefile patch?")
36533654
return False
3654-
else:
3655-
with f:
3656-
our = f.readlines()
36573655

36583656
if their == our:
36593657
return True

0 commit comments

Comments
 (0)