Skip to content

utils_net: strip ANSI escape codes before parsing JSON output#4354

Closed
dzhengfy wants to merge 1 commit intoavocado-framework:masterfrom
dzhengfy:enhance_ip_output_parse
Closed

utils_net: strip ANSI escape codes before parsing JSON output#4354
dzhengfy wants to merge 1 commit intoavocado-framework:masterfrom
dzhengfy:enhance_ip_output_parse

Conversation

@dzhengfy
Copy link
Copy Markdown
Contributor

@dzhengfy dzhengfy commented Apr 3, 2026

This PR fixes a JSON parsing issue in virttest/utils_net.py where ANSI escape sequences (like bracketed paste mode codes ^[[?2004l) in ip -json output would cause json.loads to fail.

The fix involves stripping all ANSI escape sequences from the command output string before attempting to parse it as JSON.

Signed-off-by: Dan Zheng dzheng@redhat.com

When running ip commands with -json, some environments may include
ANSI escape sequences (e.g., bracketed paste mode codes like ^[[?2004l)
in the output, which causes json.loads to fail.

This patch ensures that any ANSI escape sequences are stripped from
the output before attempting to parse it as JSON.

Signed-off-by: Dan Zheng <dzheng@redhat.com>
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces ANSI escape sequence stripping for command outputs in get_linux_iface_info and get_default_gateway_json to prevent JSON parsing errors. The reviewer suggests refactoring the duplicated regex into a module-level compiled constant to improve performance and maintainability.

Comment thread virttest/utils_net.py

try:
ip_output_str = run_func(ip_cmd).strip()
ip_output_str = re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', ip_output_str)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ANSI escape sequence regex is duplicated here and in get_default_gateway_json (line 4121). To improve maintainability and performance, consider defining this regex as a compiled constant at the module level (e.g., ANSI_ESCAPE_RE = re.compile(...)). This avoids redundant string compilation on every function call and ensures consistency across the module.

Comment thread virttest/utils_net.py

try:
ip_output_str = run_func(cmd).strip()
ip_output_str = re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', ip_output_str)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ANSI escape sequence regex is duplicated here and in get_linux_iface_info (line 3932). As mentioned previously, refactoring this into a module-level compiled constant would be more efficient and maintainable.

@dzhengfy
Copy link
Copy Markdown
Contributor Author

dzhengfy commented Apr 3, 2026

Before the fix:

[stdlog] 2026-04-02 21:13:29,188 aexpect.client client           L1412 DEBUG| Sending command (safe): ip -json addr show
[stdlog] 2026-04-02 21:13:29,431 avocado.virttest.utils_net utils_net        L3932 DEBUG| Interface info :
[stdlog] **^[[?2004l**[{"ifindex":1,"ifname":"lo","flags":["LOOPBACK","UP","LOWER_UP"],":[{"family":"inet6","local":"fe80::2509:7e53:79b7:8d42","prefixlen":64,"scope":"link","tentative":true,"noprefixroute":true,"valid_life_time":4294967295,"preferred_life_time":4294967295}]}]
[stdlog] 2026-04-02 21:13:29,432 avocado.virttest.utils_net utils_net        L3935 ERROR| Failed to get interface info via "ip" command. Reas**on: Expecting value: line 1 column 1** (char 0)

After the fix: no Error

[stdlog] 2026-04-02 23:00:21,918 aexpect.client client           L1412 DEBUG| Sending command (safe): ip -json addr show 
[stdlog] 2026-04-02 23:00:22,208 avocado.virttest.utils_net utils_net        L3933 DEBUG| Interface info :
[stdlog] [{"ifindex":1,"ifname":"lo","flags":["LOOPBACK","UP","LOWER_UP"],["enx9abf266d68d8"],"addr_info":[{"family":"inet6","local":"fe80::a76d:7149:8175:8908","prefixlen":64,"scope":"link","noprefixroute":true,"valid_life_time":4294967295,"preferred_life_time":4294967295}]}]
[stdlog] 2026-04-02 23:00:22,210 avocado.virttest.virsh virsh            L0818 DEBUG| Running virsh command: domiflist avocado-vt-vm1  

@dzhengfy dzhengfy closed this Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant