Skip to content

Fix aperture macro 0X parsed as hex float instead of multiplication#285

Merged
spe-ciellt merged 1 commit intogerbv:developfrom
SourceParts:fix/amacro-hex-expression
Feb 27, 2026
Merged

Fix aperture macro 0X parsed as hex float instead of multiplication#285
spe-ciellt merged 1 commit intogerbv:developfrom
SourceParts:fix/amacro-hex-expression

Conversation

@rampageservices
Copy link
Copy Markdown
Contributor

@rampageservices rampageservices commented Feb 26, 2026

Summary

  • Fixes gerb_fgetdouble() misparsing expressions like 0X25.4+90 as hexadecimal floating-point literals (C99 strtod interprets 0X25.4 as 37.25) instead of letting the aperture macro parser handle X as the multiplication operator
  • Adds an early check for 0x/0X prefix that returns 0.0 and advances past only the 0, so the expression correctly evaluates as 0 × 25.4 + 90 = 90
  • This is safe for all callers since Gerber format never uses hexadecimal numbers

Test plan

  • New test file test-amacro-hex-expression.gbx exercises the 0X25.4+90 expression in a LINE21 rotation parameter
  • Golden PNG test-amacro-hex-expression.png included — shows rectangle at 90° rotation (not ~127°)
  • RS-274X round-trip export confirms rotation = 90.000000 (not 127.250000)
  • Full test suite: 90 passed (up from 89), 0 new failures

Fixes #253

C99's strtod() interprets "0X25.4" as a hexadecimal floating-point
literal (= 37.25) instead of letting the parser handle X as gerbv's
multiplication operator. This causes expressions like "0X25.4+90" to
evaluate to 127.25 instead of the correct 0 * 25.4 + 90 = 90.

Add an early check in gerb_fgetdouble() to detect when the input
starts with "0x" or "0X" and return 0.0 while advancing past only
the "0". The x/X is then correctly handled as multiplication by the
aperture macro expression parser. This is safe for all callers since
Gerber format never uses hexadecimal numbers.

Fixes gerbv#253
@spe-ciellt spe-ciellt self-assigned this Feb 27, 2026
@spe-ciellt
Copy link
Copy Markdown
Contributor

Thank you for this patch. If this is good enough I apply it else I will leave comments.

@spe-ciellt
Copy link
Copy Markdown
Contributor

Looks simple enough. There is the reading more data than maybe available, but I guess it is done in more places in gerbv. An overhaul on how data is read might be in its place.
Thank you for spotting this and I will merge it.

@spe-ciellt spe-ciellt merged commit d933181 into gerbv:develop Feb 27, 2026
1 of 2 checks passed
@rampageservices rampageservices deleted the fix/amacro-hex-expression branch February 27, 2026 17:20
@spe-ciellt spe-ciellt added the bug Something isn't working label Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aperture Macro Rotation Bug

2 participants