Skip to content

Commit

Permalink
#4170: Poco::FileStream is always opened with std::ios::in | std::ios…
Browse files Browse the repository at this point in the history
…::out
  • Loading branch information
obiltschnig committed Sep 30, 2023
1 parent 1db6fb9 commit 4244c32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Foundation/include/Poco/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class Foundation_API FileIOS: public virtual std::ios
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.

void open(const std::string& path);
/// Opens the file specified by path, using the default mode given
/// in the constructor.
///
/// Throws a FileException (or a similar exception) if the file
/// does not exist or is not accessible for other reasons and
/// a new file cannot be created.

void close();
/// Closes the file stream.
///
Expand Down
19 changes: 13 additions & 6 deletions Foundation/src/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ FileIOS::~FileIOS()
void FileIOS::open(const std::string& path, std::ios::openmode mode)
{
clear();
_buf.open(path, mode | _defaultMode);
_buf.open(path, mode);

This comment has been minimized.

Copy link
@jbulow

jbulow Oct 31, 2023

This change breaks code that only specified std::ios_base::binary when opening a Poco::FileInputStream. The _defaultMode of std::ios::in set when creating the Poco::FileInputStream instance is lost resulting in a write-only stream by the implementation of open in FileStream_POSIX.cpp. On Win32 it will just result in a broken state when neither of std::ios::in and std::ios::out are set.

Example:

    Poco::FileInputStream fis;
    fis.open("Some_binary_file", std::ios_base::binary);

fis will now be a write-only stream on POSIX and a broken stream on Win32.

This comment has been minimized.

Copy link
@jbulow

jbulow Oct 31, 2023

_defaultMode should be ored with mode to get expected behavior.

}


void FileIOS::open(const std::string& path)
{
clear();
_buf.open(path, _defaultMode);
}


Expand All @@ -66,10 +73,10 @@ FileInputStream::FileInputStream():


FileInputStream::FileInputStream(const std::string& path, std::ios::openmode mode):
FileIOS(std::ios::in),
FileIOS(mode | std::ios::in),
std::istream(&_buf)
{
open(path, mode);
open(path, mode | std::ios::in);
}


Expand All @@ -86,10 +93,10 @@ FileOutputStream::FileOutputStream():


FileOutputStream::FileOutputStream(const std::string& path, std::ios::openmode mode):
FileIOS(std::ios::out),
FileIOS(mode | std::ios::out),
std::ostream(&_buf)
{
open(path, mode);
open(path, mode | std::ios::out);
}


Expand All @@ -106,7 +113,7 @@ FileStream::FileStream():


FileStream::FileStream(const std::string& path, std::ios::openmode mode):
FileIOS(std::ios::in | std::ios::out),
FileIOS(mode),
std::iostream(&_buf)
{
open(path, mode);
Expand Down

0 comments on commit 4244c32

Please sign in to comment.