AcademySoftwareFoundation / AcademySoftwareFoundation/openvdb

[BUG] Save multiple times using the same file or std::ostream with io::Stream

Open
#1,134 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
3.4k
Forks
774
Avg merge
3d 9h
Merged PRs (30d)
34

Description

### Environment
**Operating System:** Linux
**Version / Commit SHA:** OpenVDB 8.0
**Other:** compiler clang

### Describe the bug
If we try to use the same std::ostream (or file) multiple times there is a segmentation fault due to bad data returned by `pword`.

### To Reproduce
Steps to reproduce the behavior:
1. Build with OpenVDB with clang
2. Build the example below
3. Run the executable

Example:
```cpp
#include
#include
#include

#include
#include

int main ( int argc, char const *argv[] ) {
openvdb::initialize ();

// Create sphere
float radius = 5.0f;
openvdb::Vec3f center ( 1.0, 0.5, 0.3 );
float voxelSize = 0.3f;

std::vector spheres;

for ( size_t i = 0; i < 3; ++i )
spheres.push_back ( openvdb::tools::createLevelSetSphere ( radius + 0.5 * i, center, voxelSize ) );

// Save grid to file
std::ofstream outFile ( "test.vdb", std::ios_base::binary );

for ( size_t i = 0; i < 1; ++i ) {
openvdb::GridPtrVecPtr grids ( new openvdb::GridPtrVec );
grids->push_back ( spheres[ i ] );

openvdb::io::Stream stream ( outFile );
stream.setCompression ( openvdb::io::COMPRESS_NONE );
stream.write ( *grids );
}

return 0;
}
```

### Expected behavior
Each vector of grids is written to the same ostream.

### Additional context
Adding `io::clearStreamMetadataPtr(os);` at the end of `void Archive::write(std::ostream& os, const GridCPtrVec& grids, bool seekable, const MetaMap& metadata) const` in Archive.cc fixed the issue for me.

Contributor guide

Open the contributing guide

Research direction

Start in Archive.cc at Archive::write(std::ostream& os, const GridCPtrVec& grids, bool seekable, const MetaMap& metadata), then compare the stream metadata cleanup with the reported clearStreamMetadataPtr(os) change. Build OpenVDB with clang and run the supplied example; done means writing to the same file or std::ostream repeatedly no longer segfaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.