Skip to content

meta-info read fails for large transformation values while write works #2362

@sebastian-goeldi

Description

@sebastian-goeldi

Hi Matthias,

I have found a somewhat nasty bug. There seems to be a slight asymmetry in writer vs reader for trans objects. Below is an (AI slopped) minimal working example (I am not convinced the description is correct, but I assume you can easily find the actual bug based on it, so I left it in).

"""
KLayout meta-info INT32_MIN round-trip bug — confirmed on 0.30.8 / 0.30.9.
Reproduces the failure integer overflow (written by gdsfactory/kfactory):
    RuntimeError: Range overflow on integer in Layout.read

Root cause: an integer `Trans` stored in meta-info serializes its coordinates as
BARE integers, e.g.  'p'=>[trans:r0 -2147483648,-2147483648]. KLayout's variant
reader parses such a bare integer as a signed 32-bit int. -2147483648 (= INT32_MIN)
is a VALID int32, but the parser reads the magnitude 2147483648 (> INT32_MAX
2147483647) before applying the sign, so it overflows. KLayout's own writer emits
the value, but its reader cannot read it back -> genuine "write ok / read fails".
"""

import klayout.db as db

print("klayout", db.__version__)


def roundtrip(x):
    ly = db.Layout()
    ly.create_cell("TOP")
    mi = db.LayoutMetaInfo("p", db.Trans(0, False, x, x))  # integer Trans, coord = x
    mi.persisted = True
    ly.add_meta_info(mi)
    ly.write("/tmp/tr.gds")  # WRITE always succeeds
    seg = open("/tmp/tr.gds", "rb").read()
    i = seg.find(b"META")
    try:
        db.Layout().read("/tmp/tr.gds")  # READ may fail
        print(f"  x={x:>12}  file={seg[i : i + 24]!r}...  READ OK")
    except BaseException as e:
        print(f"  x={x:>12}  file={seg[i : i + 24]!r}...  *** {type(e).__name__}: {e}")


print("[int Trans coordinate round-trip through GDS meta-info]")
roundtrip(-2147483647)  # INT32_MIN + 1  -> OK
roundtrip(-2147483648)  # INT32_MIN      -> fails on read

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions