Skip to content

XPM: Allow / in array name - #58

Open
akashchamp wants to merge 1 commit into
image-rs:mainfrom
akashchamp:xpm-slash-in-name
Open

akashchamp wants to merge 1 commit into
image-rs:mainfrom
akashchamp:xpm-slash-in-name

Conversation

@akashchamp

@akashchamp akashchamp commented Oct 30, 2026 •

Copy link
Copy Markdown

Fixes #57.

Some tools write the source file path as the array name (static char */tmp/K9Duvy1kRf/page-001[] = {), which failed with "Failed to parse array definition" because the / was taken as the start of a comment.

This now follows libXpm's header handling instead of parsing the C declaration. In xpmParseHeader (src/data.c), libXpm only checks that the second word of the first line is XPM (for XPM 3), then calls xpmNextString with " as the string start, which skips everything up to the first "; a / followed by * starts a comment (ParseComment) that is skipped up to */, so a " inside a comment does not count. The declaration itself (static, const, char, *, the name, [] = {) is never looked at.

Here the /* XPM */ check is unchanged, and skip_to_first_string replaces the declaration parsing: it skips to the first " with the same comment handling, using the existing one byte of lookahead. read_keyword, read_name, read_to_newline and skip_non_empty_whitespace_and_comments were only used by the old declaration parsing and are removed. The rest of the decoder is unchanged, and the module docs no longer list static char ** name = { as rejected. Files are still rejected if the first line is not /* XPM */ or if there is no string before EOF outside a comment.

Added tests/images/xpm/green_slash_name.xpm (green.xpm with the header line from the issue, same reference PNG) and unit tests for a / in the name, a comment containing " inside the name, and the rejected headers above.

Checked with:

  • cargo test: the new fixture and the slash/comment unit tests fail on main (the fixture with the exact error from the issue, byte=24,line=2:col=14) and pass with this change; the existing XPM test images still match their reference PNGs
  • cargo clippy --all-features --all-targets -- -D warnings and cargo fmt --check
  • cargo run --example convert -- tests/images/xpm/green_slash_name.xpm out.png writes the 5x5 PNG

@mstoeckl

mstoeckl commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

I generally don't like increasing lookahead (which will be expensive when parsing large images) just for the specific edge case that we see now in #57; I wouldn't be surprised if there are other files that deviate from being correct in strange ways, because historical parsers (see comment in #57 (comment)) would accept most anything. It may be quite a big change, but I think explicitly introducing a SpecCompliance decoder parameter (see image-rs/image#2767) and then using to select a simpler header decoding step may both keep the implementation here relatively simple and general, while keeping it useful to actually validate XPM images or and possible ambiguity.

As for which of the many mutually incompatible header parsing approaches to use... I think getting closer to libxpm's behavior (see #57 (comment)) is least likely to break on historical/hand written images.

Some tools write the source file path as the array name, e.g.
`static char */tmp/K9Duvy1kRf/page-001[] = {`. Instead of parsing the
declaration, skip everything after `/* XPM */` up to the first string,
honoring C comments, as libXpm does.

Fixes image-rs#57
@akashchamp

Copy link
Copy Markdown
Author

Thanks, I reworked this to follow libXpm's header handling: after the /* XPM */ check, everything up to the first " is skipped without being parsed, honoring /* */ comments the way xpmNextString/ParseComment do, so the declaration and name (or a file path) are never looked at. It needs no extra lookahead beyond the one byte TextReader already has. I left SpecCompliance out since it (image#2767) isn't in a released image version yet; a stricter header mode can be layered on top later if wanted.

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.

Loading error for some XPM files

2 participants