Skip to content

API change support and whole rework to find a Windows crash before.#86

Merged
AlwinEsch merged 13 commits into
xbmc:Piersfrom
AlwinEsch:Piers-change
May 22, 2026
Merged

API change support and whole rework to find a Windows crash before.#86
AlwinEsch merged 13 commits into
xbmc:Piersfrom
AlwinEsch:Piers-change

Conversation

@AlwinEsch

@AlwinEsch AlwinEsch commented May 15, 2026

Copy link
Copy Markdown
Member

Add-on update related to Kodi API change defined on xbmc/xbmc#28278

Thought about making the API change and noticed that the add-on always crashes under Windows. At the beginning I couldn't figure out where this crash was coming from and reworked the code to find the cause at some point.

At the end I realized that the crash came from reading "ReadVectors" and std::fstream vectorsfile(filename.str().c_str(), std::fstream::in);. Somehow the data read was not correct on Windows and led to a crash in mathematical processes (in blurengine.cpp).

AlwinEsch added 6 commits May 15, 2026 02:52
As the fische is a independent code where not access Kodi's API can it be in
own folder to have more clean and prepared for further changes.

Further becomes it easier to take the code by someone to places outside of Kodi.
As this goes with ASM deep in hardware, makes it sense for me to leave as "C" and not as "C++".
From my side hold the code there as small as possible. Further is it independent to all other places.
This done to have better usable in coming changes.
As the fische code by add-on was on begin defined as a "C" code only and over the year some "C++" parts implemented to them.
About this and to become a usable "C" ABI support back to them are the headers renamed and to identify in furture which relates to "C" and whicht to "C++".
To makes it a bit cleaner and not have the #ifdef and #endif.
@AlwinEsch
AlwinEsch requested a review from garbear May 15, 2026 03:29
@kodiai

kodiai Bot commented May 15, 2026

Copy link
Copy Markdown
Kodiai Review Summary

What Changed

This PR addresses a Windows crash caused by std::fstream reading vector field data incorrectly, replacing it with Kodi VFS API for cross-platform binary file I/O. The code was also reorganized, moving files into a src/fische/ directory structure. Requested minimal review; timeout risk auto-reduced the effective review to minimal.

Reviewed: core logic, file I/O

Strengths

  • ✅ Replaces platform-dependent std::fstream with Kodi VFS API for reliable binary file I/O across Windows/Linux/macOS (src/fishbmc_addon.cpp:395-428)
  • ✅ Proper error logging added for file operations with specific failure conditions (lines 407, 414, 422)

Observations

Impact

[CRITICAL] src/fishbmc_addon.cpp (418-419): Unchecked malloc causes null pointer dereference when system is out of memory
The malloc at line 418 returns nullptr when allocation fails, but line 419 immediately writes to the pointer via vectorsfile.Read(*data, size_t(size)) without checking, causing a crash when memory is exhausted.

[CRITICAL] src/fische/vectorfield.cpp (57-58): Unchecked malloc leads to crash in vector generation
Line 57 allocates up to 160 MB (N_FIELDS * m_fieldsize for 2048x2048 resolution) without checking if malloc returns nullptr. When allocation fails, FillField at line 68 dereferences the null pointer, and later blurengine.cpp:111 crashes when using the nullptr vector field.

[MAJOR] src/fische/vectorfield.cpp (44-52): Loaded vector data lacks validation, risking buffer overflow
Vector fields loaded from disk at line 45 are used directly without bounds validation. Generated vectors are validated by Validate() (line 413), but loaded vectors skip this check. In blurengine.cpp:128, unchecked vector_x/vector_y values drive pointer arithmetic that can access out-of-bounds memory if the file is corrupted—the exact crash scenario this PR fixes. While the VFS API prevents Windows I/O corruption, validation would guard against file corruption from any source.

Suggestions

  • Future consideration: Add a validation pass for loaded vector data to prevent crashes from file corruption regardless of source
  • Optional: Set *data = nullptr after free(*data) in ReadVectors error path (line 423) to prevent dangling pointers

Verdict

🔴 Address before merging -- 3 blocking issue(s) found (2 CRITICAL, 1 MAJOR)

The two critical malloc checks are essential to prevent crashes under low-memory conditions. The major validation issue would prevent future file corruption bugs.

Review Details
  • Review plan: ready hash=816fa3dd5558 route=standard task=review.full files=45 lines=5681(local-diff) budget=75t/948s gates=3/3 publish=canonical-visible-surface graph=skipped candidates=preferred

  • Review reducer: ready input=3 kept=3 suppressed=0 rewritten=2 deprioritized=0 lowConfidence=0 auditEvents=1 severityDemoted=0 graphValidated=0 graphUncertain=0

  • Review candidates: shadow recorded=3 rejected=0 errors=0 artifact=present repo=xbmc-visualization.fishbmc pr=86 key=kodiai-review-output:v1:inst-109141824:xbmc-visualization.fishbmc:pr-86:action-o delivery=3f5b5370-500e-11f1-80ea-d59fa9bb8abe

  • Review candidate publication: mode=direct-fallback approved=1 rewritten=2 published=0 directFallback=1 reasons=candidate-publisher-skipped,direct-fallback-attempted,direct-fallback-published,direct-fallback-disallowed

  • Files reviewed: 45

  • Findings: 2 critical, 1 major, 0 medium, 0 minor (includes 3 from summary observations)

  • Lines changed: +2548 -3133

  • Requested profile: minimal (auto, lines changed: 5681)

  • Effective profile: minimal

  • Timeout auto-reduction: applied

  • Contributor experience: profile-backed (using linked contributor profile guidance)

  • Review completed: 2026-05-15T03:35:43.568Z

  • Total wall-clock: 6m 28s

  • Phase timings:

    • queue wait: 1ms
    • workspace preparation: 778ms
    • retrieval/context assembly: 2.7s
    • executor handoff: 8.3s
    • remote runtime: 6m 13s
    • publication: 1.2s
  • Tokens: 185 in / 16,587 out | 0.6427

  • Keyword parsing: No keywords detected

@AlwinEsch AlwinEsch mentioned this pull request May 15, 2026
AlwinEsch added 3 commits May 18, 2026 13:16
Relates to:
```
  CMake Deprecation Warning at CMakeLists.txt:2 (cmake_policy):
    Compatibility with CMake < 3.10 will be removed from a future version of
    CMake.
```

And to have equal to Kodi's used version.
Some parts in mathematic processes are leaved on the old "C" way.
Other parts are in C++ now, reason why done is to have more OS independent and more clean in Code.

Seen that fische in his Version 3 was based complete upon C++, but done here from scratch without code take from his old version.

See here version 3: https://sources.debian.org/src/fische/3.2.2-8/src
And here version 4: https://github.com/maysl/fische
Normally not needed on Kodi, but thought to have it as lib usable in "C" too.
Tested temporary by set add-on code to use as "C" of it and has worked.
@AlwinEsch

Copy link
Copy Markdown
Member Author

@kodiai review this please

Comment thread src/fische/vector.cpp
Comment thread src/fische/analyst.cpp
Comment thread src/fische/vectorfield.cpp
Comment thread src/fische/vectorfield.cpp Outdated
Comment thread src/fische/vectorfield.cpp Outdated
Comment thread src/fische/vectorfield.cpp Outdated
Comment thread src/fishbmc_addon.cpp
@kodiai

kodiai Bot commented May 18, 2026

Copy link
Copy Markdown
Kodiai Review Summary

What Changed

This PR updates the add-on for Kodi API changes and fixes a Windows crash related to ReadVectors file I/O by replacing std::fstream with Kodi's VFS API.

Reviewed: core logic, docs

Strengths

  • ✅ Replaced std::fstream with Kodi's VFS API (src/fishbmc_addon.cpp:400-442), addressing the Windows crash mentioned in the PR description
  • ✅ Added MD5 hash validation for vector file integrity (src/fishbmc_addon.cpp:431-433), preventing corrupted data from being loaded
  • ✅ Proper memory management with malloc/free for C interop compatibility (src/fische/vectorfield.cpp:57, 92)

Observations

Impact

[CRITICAL] src/fische/vector.cpp (29-34): Division by zero when vector length is zero
Causes crash at center pixel where x == m_center_x && y == m_center_y (called from vectorfield.cpp:170)

[CRITICAL] src/fische/analyst.cpp (51): Division by zero when no beat gaps match criteria
If no gaps satisfy the filter at lines 41-48, count remains 0 causing division by zero

[CRITICAL] src/fische/vectorfield.cpp (45-51): Missing validation for ReadVectors return value
(1) bytes < m_fieldsize causes m_n_fields = 0, leading to modulo-by-zero at line 103; (2) unaligned bytes causes truncated field count; (3) no size sanity check

[MAJOR] src/fische/vectorfield.cpp (103): Thread-safety issue with rand()
Uses non-thread-safe rand() instead of rand_r(&m_rand_seed) used elsewhere in the codebase (lines 109, 111, 260, 262)

[MAJOR] src/fishbmc_addon.cpp (419): Memory leak when hash validation fails
If malloc succeeds but hash check at line 432 fails, allocated memory is not freed before throwing exception

Preference

[MEDIUM] src/fische/vectorfield.cpp (124): Boundary validation inconsistency
Optional: Use m_height - 3 instead of m_height - 2 for consistency with the x-axis validation pattern at line 120

[MEDIUM] src/fische/vectorfield.cpp (133): Magic number in pointer arithmetic
Optional: Replace magic number 2 with sizeof(uint16_t) for clarity

Verdict

🔴 Address before merging -- 5 blocking issue(s) found (CRITICAL/MAJOR)

@AlwinEsch

Copy link
Copy Markdown
Member Author

@kodiai review this please

@kodiai

kodiai Bot commented May 19, 2026

Copy link
Copy Markdown
Kodiai Review Summary

What Changed

API update for Kodi changes and comprehensive rework to fix Windows crash related to vector file reading using std::fstream, now replaced with kodi::vfs::CFile for cross-platform compatibility.

Reviewed: core logic, docs

Strengths

  • ✅ Replaced problematic std::fstream with kodi::vfs::CFile for cross-platform file I/O (fishbmc_addon.cpp:408-439)
  • ✅ Added MD5 hash verification to detect corrupted vector files before use (fishbmc_addon.cpp:431-436)
  • ✅ Proper error handling with try-catch blocks and resource cleanup in ReadVectors (fishbmc_addon.cpp:409-445)

Observations

Impact

[CRITICAL] src/fische/vectorfield.cpp (42-58): Missing validation for loaded vector data causes potential buffer overflow
Vectors loaded from files bypass validation, allowing malicious or corrupted data to cause buffer overflows when used in blurengine.cpp ThreadWorker for pointer arithmetic. The Validate function (lines 121-131) ensures generated vectors are safe, but loaded vectors skip this check.

[CRITICAL] src/fische/blurengine.cpp (123-135): Missing bounds validation enables buffer overflow from vector data
Pointer arithmetic using unvalidated vector offsets (line 128: source + (y + vector_y) * m_width + x + vector_x) can access memory outside the allocated buffer. Lines 132-135 access ±2 row/column offsets without bounds checking, causing crashes when vectors contain out-of-bounds values.

[MAJOR] src/fische/blurengine.cpp (96): Incorrect type for vector components causes wrong value interpretation
Using int_fast8_t instead of int8_t for vector_x and vector_y causes incorrect value interpretation when int_fast8_t is wider than 8 bits (e.g., int32_t on some platforms). Vectors are encoded as int8_t values (vector.cpp:65-69), so uint8_t value 255 should be -1 but becomes 255 in int32_t.

[MAJOR] src/fische/vectorfield.cpp (54-57): Missing nullptr assignment after free on error path
After freeing m_fields on line 56, it's not set to nullptr, creating a potential dangling pointer if subsequent malloc fails and execution continues abnormally.

[MAJOR] src/fische/vectorfield.cpp (71-92): Null pointer dereference risk after malloc failure
If malloc fails on line 62, m_field is set to nullptr on line 92, but fische.cpp:203 calls Field() without checking if it's null, potentially causing crashes.

Preference

[MEDIUM] src/fishbmc_addon.cpp (415-421): Missing size validation allows excessive memory allocation
Optional: ReadVectors allocates memory based on file size without validating the size is reasonable. Add a maximum size check (e.g., 20 fields × 2048 × 2048 × 2 bytes) before malloc to prevent denial of service from malicious files.

[MEDIUM] src/fische/vectorfield.cpp (73-90): WriteVectors called even when vector generation was cancelled
Optional: Lines 86-88 write vectors even if cancel was set during generation (lines 75-79), potentially saving incomplete data that will cause crashes when loaded later. Only call WriteVectors if !m_cancelled.

Suggestions

  • Future consideration: Add unit tests for vector file loading with corrupted/malicious data to prevent regressions
  • Future consideration: Consider using RAII (std::unique_ptr with custom deleter) instead of raw malloc/free for safer memory management

Verdict

🔴 Address before merging -- 5 blocking issue(s) found (CRITICAL/MAJOR)

@AlwinEsch

Copy link
Copy Markdown
Member Author

The last AI notes I'm mostly ignored now, some of them makes not directly sense and some of them go into too much detail for me.
Is just implementing in C++ from the previous code where it started and runned like that.

I'll leave it as it is now. Gotta finish it.

For a for me unknown reason was by this "std::fstream vectorsfile(filename.str().c_str(), std::fstream::in);"
something partly wrong readed where creates after few seconds to one minute by playback a crash of Kodi.
Due a strange reason this comes only on Windows. By fault search I was not come on begin to them, thought on begin
maybe relates to threads and why rewritten the code to find why it crash.

With use of our Kodi filesystem API has it worked correct.
AlwinEsch added 3 commits May 21, 2026 22:26
This the reason why started on add-on and to have usable on coming API change of Kodi.
@AlwinEsch
AlwinEsch merged commit 95985c1 into xbmc:Piers May 22, 2026
8 checks passed
@AlwinEsch
AlwinEsch deleted the Piers-change branch May 22, 2026 00:04
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