Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint labels can break CQM (de-)serialization #1358

Open
randomir opened this issue Sep 7, 2023 · 0 comments · May be fixed by #1359
Open

Constraint labels can break CQM (de-)serialization #1358

randomir opened this issue Sep 7, 2023 · 0 comments · May be fixed by #1359
Labels

Comments

@randomir
Copy link
Member

randomir commented Sep 7, 2023

Description

Currently CQM.to_file() encodes constraint labels with json.dumps, allowing characters that are illegal in file names, like /. That becomes a problem only when trying to extract the CQM zip file.

However, CQM.from_file() assumes forward slashes are not allowed in file names and that / is used only as (POSIX) path separator:

for arch in zf.namelist():
# even on windows zip uses /
match = re.match("constraints/([^/]+)/", arch)

Consequently, decoding CQMs with slashes in constraint labels will fail.

Steps To Reproduce

>>> import dimod
>>> cqm = dimod.CQM()
>>> cqm.add_constraint(dimod.Binary() == 1, label='a/b')
'a/b'
>>> dimod.CQM.from_file(cqm.to_file())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<omitted>/env311/lib/python3.11/site-packages/dimod/constrained/constrained.py", line 1078, in from_file
    label = deserialize_variable(json.loads(constraint))
                                 ^^^^^^^^^^^^^^^^^^^^^^
  File "<omitted>/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<omitted>/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<omitted>/lib/python3.11/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1 (char 0)

Expected Behavior

Serializing CQMs with forward slashes (and some other unicode characters, like NULL) should be forbidden. Or at least such labels should be renamed, either on constraint creation, or cqm serialization.

Environment

  • any OS, any python
  • reproduces with latest dimod==0.12.12
@randomir randomir added the bug label Sep 7, 2023
arcondello added a commit to arcondello/dimod that referenced this issue Sep 13, 2023
arcondello added a commit to arcondello/dimod that referenced this issue Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant