Skip to content

Fix build error by replacing stbi_write_png_to_mem with `stbi_write…#9

Merged
pedroac merged 2 commits intomainfrom
release
Mar 27, 2026
Merged

Fix build error by replacing stbi_write_png_to_mem with `stbi_write…#9
pedroac merged 2 commits intomainfrom
release

Conversation

@pedroac
Copy link
Copy Markdown
Owner

@pedroac pedroac commented Mar 27, 2026

…_png_to_func`

@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Mar 27, 2026

PR Summary

  • Changed How Image Data is Stored
    Instead of using a raw pointer – an older method of memory storage – we have switched to using a std::vector<unsigned char>. This software object simplifies memory handling, reducing the risk of programming errors that could cause the software to crash.

  • Added a New Function to Write Image Data
    This update introduces a new function, write_to_vec, that adds image data directly into our storage vector.

  • Corrected Size Calculation for Data Storage
    Previously, the parameter for the archive_entry_set_size function used the size of the old raw pointer. Now, it accurately uses the size of our image storage vector, refining how we track how much memory we're using.

  • Adjusted How Data is Written
    Finally, we have altered the archive_write_data function, which is responsible for recording our image data. Instead of writing from the old data handling method, it now writes from our newly implemented vector. This ensures any data writing will accurately reflect our upgraded storage method.

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 refactors the PNG encoding logic in SpriteUnpacker to use stbi_write_png_to_func with a std::vector instead of stbi_write_png_to_mem with a raw buffer, which improves memory management and idiomatic C++ usage. A review comment suggests adding a safety check for the size parameter in the write callback to ensure robustness against invalid inputs during vector insertion.

Comment on lines +625 to +629
auto write_to_vec = [](void* context, void* data, int size) {
auto* vec = static_cast<std::vector<unsigned char>*>(context);
const auto* bytes = static_cast<const unsigned char*>(data);
vec->insert(vec->end(), bytes, bytes + size);
};
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 stbi_write_png_to_func callback receives size as an int. While it's unlikely to be negative in a successful scenario, adding a check to handle non-positive size values would make the callback more robust and prevent potential undefined behavior from std::vector::insert if an invalid size is ever passed.

        auto write_to_vec = [](void* context, void* data, int size) {
            if (size <= 0) {
                return;
            }
            auto* vec = static_cast<std::vector<unsigned char>*>(context);
            const auto* bytes = static_cast<const unsigned char*>(data);
            vec->insert(vec->end(), bytes, bytes + size);
        };

- Replace stbi_write_png_to_mem with stbi_write_png_to_func in
  spratunpack_command.cpp to avoid "undeclared identifier" errors in some
  stb_image_write.h versions.
- Reorder CMakeLists.txt to detect dependencies (libarchive, gettext, zopflipng)
  before defining targets. This fixes "archive.h not found" errors on macOS where
  headers are in non-standard brew paths.
- Remove redundant source file inclusion in executable targets by linking to
  spratcore library.
@pedroac pedroac merged commit c8c5fcf into main Mar 27, 2026
10 checks passed
@pedroac pedroac deleted the release branch March 27, 2026 01:18
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