Skip to content

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions