Bug 2091636

Summary: python3-dnf wrong indentation level in substitutions.py:64
Product: Red Hat Enterprise Linux 9 Reporter: space88man <shihping.chan>
Component: dnfAssignee: Jaroslav Mracek <jmracek>
Status: CLOSED ERRATA QA Contact: Tomáš Bajer <tbajer>
Severity: unspecified Docs Contact:
Priority: medium    
Version: 9.0CC: james.antill, mbanas, mblaha, tbajer
Target Milestone: rcKeywords: Triaged
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: dnf-4.14.0-1.el9 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of:
: 2110912 (view as bug list) Environment:
Last Closed: 2023-05-09 08:18:18 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2110912    

Description space88man 2022-05-30 14:34:45 UTC
Description of problem:

python3-dnf:
If the vars directory happens to have a non-file, the var val is not set
and an uncaught Exception is triggered


Version-Release number of selected component (if applicable):
4.10.0-5.el9_0

How reproducible:
Depends on how python parses the varsdir

Steps to Reproduce:
1. In the dnf varsdir add random non-files (like links)
2.
3.

Actual results:
Traceback (most recent call last):
  File "/bin/reposync", line 101, in <module>
    main.user_main(MAPPING[command] + args, exit_code=True)
  File "/usr/lib/python3.9/site-packages/dnf/cli/main.py", line 201, in user_main
    errcode = main(args)
  File "/usr/lib/python3.9/site-packages/dnf/cli/main.py", line 67, in main
    return _main(base, args, cli_class, option_parser_class)
  File "/usr/lib/python3.9/site-packages/dnf/cli/main.py", line 102, in _main
    cli.configure(list(map(ucd, args)), option_parser())
  File "/usr/lib/python3.9/site-packages/dnf/cli/cli.py", line 799, in configure
    self._read_conf_file(opts.releasever)
  File "/usr/lib/python3.9/site-packages/dnf/cli/cli.py", line 933, in _read_conf_file
    subst.update_from_etc(from_root, varsdir=conf._get_value('varsdir'))
  File "/usr/lib/python3.9/site-packages/dnf/conf/substitutions.py", line 64, in update_from_etc
    self[fsvar] = val
UnboundLocalError: local variable 'val' referenced before assignment



Expected results:
Non-files are ignored

Additional info:

The indentation level at line 64 looks wrong perhaps?
     44     def update_from_etc(self, installroot, varsdir=("/etc/yum/vars/", "/etc/dnf/vars/")):
     45         # :api
     46 
     47         for vars_path in varsdir:
     48             fsvars = []
     49             try:
     50                 dir_fsvars = os.path.join(installroot, vars_path.lstrip('/'))
     51                 fsvars = os.listdir(dir_fsvars)
     52             except OSError:
     53                 continue
     54             for fsvar in fsvars:
     55                 filepath = os.path.join(dir_fsvars, fsvar)
     56                 if os.path.isfile(filepath):
     57                     try:
     58                         with open(filepath) as fp:
     59                             val = fp.readline()
     60                         if val and val[-1] == '\n':
     61                             val = val[:-1]
     62                     except (OSError, IOError) as exc:
     63                         continue
     64                 self[fsvar] = val

Comment 1 space88man 2022-05-30 14:39:31 UTC
To trigger this error I added symbolic links to directories; the error depends on how python scans the directory.

1. If python encounters a non-file (e.g link to directory) the error will be triggered
2. if python encounters a file, the var val will be set from this file, so in future interations self[fsvar] = val will work as it will reuse the last successful value from readline()

Comment 3 Jaroslav Mracek 2022-07-11 10:41:25 UTC
I created a PR that resolves the issue - https://github.com/rpm-software-management/dnf/pull/1834. The bug still requires a test for our CI.

Comment 4 Jaroslav Mracek 2022-07-26 07:46:14 UTC
CI tests: https://github.com/rpm-software-management/ci-dnf-stack/pull/1136

Comment 14 errata-xmlrpc 2023-05-09 08:18:18 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (dnf bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2023:2490