Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Stacktrace on badly formed CAN_ErrorFrame (?)

未关闭
#1,913 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
data

调研方向

从 can/io/mf4.py 约第 410 行开始,这里使用报告的 DataLength 对 CAN_ErrorFrame.DataBytes 进行索引。使用 Kvaser MF4 文件重现问题,并在该访问之前检查 CAN_ErrorFrame.DataBytes 的形状和值。完成标准是 can.LogReader 消费错误帧时不引发 IndexError,并且提供的迭代能够正常工作。

由索引模型根据 Issue 内容生成。

描述

bug

Describe the bug

We have an MF4 file from Kvaser that causes the following stack trace when run:

Traceback (most recent call last):
  File "load.py", line 43, in <module>
    sys.exit(main(args))
  File "load.py", line 27, in main
    for msg in can.LogReader(args.mf4_file):
  File "/home/john/.pyenv/versions/3.8.20/lib/python3.8/heapq.py", line 373, in merge
    value = next()
  File "/home/john/tmp/keep_1y/python-can/can/io/mf4.py", line 410, in __iter__
    kv["data"] = data["CAN_ErrorFrame.DataBytes"][i][
IndexError: invalid index to scalar variable.

The test code is essentially:

for msg in can.LogReader(mf4_file):
    if msg.is_error_frame:
        count["error"] += 1
    else:
        count["other"] += 1

I put some debug at site of the error (line 407 of can/io/mf4.py):

data_length=4
data["CAN_ErrorFrame.DataBytes"][i]=16777216

2^24?

To Reproduce

I was unable to create a file with python-can with the error data.

Expected behavior

No crash. :)

Additional context

OS and version: Linux
Python version: 3.8.20
python-can version: 654a02ae 2024-11-28 | Changelog for v4.5.0 (#1897) (HEAD -> main, origin/main, origin/HEAD)
python-can interface/s (if applicable): None

Traceback and logs

If I make this hack it consumes the error frame:

diff --git a/can/io/mf4.py b/can/io/mf4.py
index 4f5336b4..099395b4 100644
--- a/can/io/mf4.py
+++ b/can/io/mf4.py
@@ -405,9 +405,9 @@ class MF4Reader(BinaryIOMessageReader):
                         and "CAN_ErrorFrame.DataBytes" in names
                     ):
                         data_length = int(data["CAN_ErrorFrame.DataLength"][i])
-                        kv["data"] = data["CAN_ErrorFrame.DataBytes"][i][
+                        kv["data"] = data["CAN_ErrorFrame.DataBytes"][i].tobytes()[
                             :data_length
-                        ].tobytes()
+                        ]

                     yield Message(**kv)
主要语言
Python
星标
1.6k
派生
697
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

hardbyte/python-can 的其他 Issue

查看 hardbyte/python-can 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。