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
分支
700
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 摘要。