Do not silently read a different file for a non-existent path with a suffix (fixes #328)#334
Open
gaoflow wants to merge 1 commit into
Open
Do not silently read a different file for a non-existent path with a suffix (fixes #328)#334gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
The 'file' branch of CDF.__init__ fell back to path.with_suffix('.cdf')
when the given path was not a file. with_suffix *replaces* the last
suffix, so a non-existent path that already has one (e.g.
'mydata.cdfINVALID') silently resolved to 'mydata.cdf' and read the
wrong file instead of raising. Only apply the '.cdf' fallback when the
path has no suffix (the 'user omitted the extension' case it was meant
for). Adds a regression test. Fixes lasp#328.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (#328)
cdflib.cdfread.CDF()can read a CDF from an illegal path built from a legal path plus extra characters. For example, passingmydata.cdfINVALID(which does not exist) silently readsmydata.cdfinstead of raising:This is a data-integrity hazard: a user can get data from a file other than the one they asked for, with no indication anything went wrong. (Reported by @ErikPGJ for JUICE/RPWI files; the upper character limit they observed before an error simply corresponds to the OS path-length limit hit in
Path.is_file().)Cause
In the
filebranch ofCDF.__init__:Path.with_suffix('.cdf')replaces the last suffix rather than appending one, somydata.cdfINVALID→mydata.cdf. The fallback was presumably meant for the "user passedmydatawithout the.cdfextension" case, but it also rewrites any wrong-but-present extension to.cdf.Fix
Only apply the
.cdffallback when the path has no suffix:mydata(no suffix) → still falls back tomydata.cdf(behavior preserved).mydata.cdfINVALID(has a suffix) → no longer clobbered →FileNotFoundError.mydata.cdfis found before the fallback is ever reached, so valid reads are unaffected.(Also drops a duplicated
self.file = pathline in the same block.)Tests
Adds
test_nonexist_path_with_extra_suffix_errors, which asserts that a real test-file path with extra characters appended raisesFileNotFoundError. It fails onmain(DID NOT RAISE) and passes with the fix;tests/test_cdfread.pypasses (5 passed). Verified the no-suffix fallback still resolves<name>→<name>.cdf.Fixes #328.