Skip to content

[Code scan] Truncate rewritten training JSON after HDF5 conversion #1909

Description

@njzjz

This issue is from a Codex global scan of the repository.

convert_training_data_to_hdf5() can leave stale bytes at the end of rewritten JSON input files.

Evidence:

dpgen/dpgen/util.py

Lines 119 to 142 in 7af5246

with open(ii, "r+") as f:
jinput = json.load(f)
if "training_data" in jinput["training"]:
# v2.0
p_sys = jinput["training"]["training_data"]["systems"]
else:
# v1.x
p_sys = jinput["training"]["systems"]
for ii, pp in enumerate(p_sys):
if "#" in pp:
# HDF5 file
p1, p2 = pp.split("#")
ff = os.path.normpath(str((dd / p1).absolute().relative_to(cwd)))
pp = ff + "#" + p2
new_pp = os.path.normpath(os.path.relpath(ff, h5_dir)) + p2
else:
pp = os.path.normpath(str((dd / pp).absolute().relative_to(cwd)))
new_pp = os.path.normpath(os.path.relpath(pp, h5_dir))
p_sys[ii] = (
os.path.normpath(os.path.relpath(h5_file, dd)) + "#/" + str(new_pp)
)
systems.append(pp)
f.seek(0)
json.dump(jinput, f, indent=4)

The function opens input JSON files with r+, updates the training system paths, calls f.seek(0), and writes the new JSON with json.dump(). It never calls f.truncate(). If the rewritten JSON is shorter than the original file, trailing bytes from the old content remain and the JSON file can become invalid.

Expected behavior: truncate the file after writing, or write to a temporary file and replace the original atomically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions