Conversation
Fixes deepmodeling#7562. The binary wavefunction writers (wfc_nao_write2file and wfc_nao_write2file_complex) used to only print a warning when the output file could not be opened, then kept writing through a null FILE pointer, which could crash or produce corrupt files. Binstream::operator<< and Binstream::write now check that the stream is open and verify the fwrite return value, matching the existing read-path behavior. The call sites in write_wfc_nao.cpp now terminate with WARNING_QUIT on open failure instead of continuing. Tests added: - BinstreamTest.WriteToUnopenedFile - ModuleIOTest.WriteWfcNaoBinaryOpenFail - ModuleIOTest.WriteWfcNaoComplexBinaryOpenFail
AsTonyshment
left a comment
Collaborator
There was a problem hiding this comment.
Some small suggestions.
| std::cout<<"Error in Binstream: cannot write to an unopened file."<<std::endl; | ||
| exit(1); | ||
| } | ||
| size_t ch = fwrite(&data,size,1,this->fileptr); |
Collaborator
There was a problem hiding this comment.
fwrite() can report success while data is still buffered, and both close() and the destructor ignore fclose() failures. With RLIMIT_FSIZE=1, both NAO binary writers return normally with only one byte written. Maybe also check the final close result and cover delayed write failures; the array overload has the same gap.
| std::cout<<"Error in Binstream: cannot write to an unopened file."<<std::endl; | ||
| exit(1); | ||
| } | ||
| size_t ch = fwrite(data,size,n,this->fileptr); |
| size_t ch = fwrite(&data,size,1,this->fileptr); | ||
| if(ch<1) | ||
| { | ||
| std::cout<<"Error in Binstream: Some data didn't be written."<<std::endl; |
Collaborator
There was a problem hiding this comment.
Some grammar errata: maybe "Some data wasn't written."?
| size_t ch = fwrite(data,size,n,this->fileptr); | ||
| if(ch<static_cast<size_t>(n)) | ||
| { | ||
| std::cout<<"Error in Binstream: Some dynamic memory didn't be written."<<std::endl; |
Collaborator
There was a problem hiding this comment.
"Some dynamic memory wasn't written."?
This branch has not been deployed
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.
Fixes #7562.
The binary wavefunction writers (wfc_nao_write2file and wfc_nao_write2file_complex) used to only print a warning when the output file could not be opened, then kept writing through a null FILE pointer, which could crash or produce corrupt files.
Binstream::operator<< and Binstream::write now check that the stream is open and verify the fwrite return value, matching the existing read-path behavior. The call sites in write_wfc_nao.cpp now terminate with WARNING_QUIT on open failure instead of continuing.
Tests added:
Reminder
AGENTS.mdanddocs/developers_guide/agent_governance.md.source/changes.Linked Issue
Fix #
Unit Tests and/or Case Tests for my changes
What's changed?
Governance Notes