Skip to content

LocalFileIdentifiableStore.__iter__ uses rstrip instead of removesuffix and lacks .json filter #499

@zrgt

Description

@zrgt

Summary

Severity: High
File: sdk/basyx/aas/backend/local_file.py:163-164

Description

Two bugs on one line crash the iterator for any non-JSON file in the store directory.

for name in os.listdir(self.directory_path):
    yield self.get_identifiable_by_hash(name.rstrip(".json"))

a) Wrong string method. str.rstrip(chars) strips any trailing character that appears in the chars string, not the literal suffix. For the .json argument, the character set is {'.', 'j', 's', 'o', 'n'}. On SHA-256 hex filenames this works by coincidence (hex chars a-f don't overlap), but any future change to the naming scheme or any filename whose hash ends in those characters would corrupt the key. The correct call is name.removesuffix(".json") (Python ≥ 3.9).

b) No extension filter. os.listdir() returns all directory entries: .DS_Store, temp files, subdirectories, etc. For non-.json entries, get_identifiable_by_hash() constructs a path like {dir}/{name}.json, which doesn't exist, and raises KeyError. One stray file in the store directory crashes the iterator.

Fix

for name in os.listdir(self.directory_path):
    if name.endswith(".json"):
        yield self.get_identifiable_by_hash(name.removesuffix(".json"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsdkSomething to do with the `sdk` package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions