diff --git a/include/ftl/protocol/packet.hpp b/include/ftl/protocol/packet.hpp index 0227c7180af362d868e834834a5a35cd67601b4b..4994194851bc66254de0004fb74609b4eb88c609 100644 --- a/include/ftl/protocol/packet.hpp +++ b/include/ftl/protocol/packet.hpp @@ -101,10 +101,10 @@ struct StreamPacket { inline int frameNumber() const { return (version >= 4) ? frame_number : streamID; } inline size_t frameSetID() const { return (version >= 4) ? streamID : 0; } - int64_t localTimestamp; // Not message packet / saved - mutable unsigned int hint_capability; // Is this a video stream, for example - size_t hint_source_total; // Number of tracks per frame to expect - int retry_count = 0; // Decode retry count + int64_t localTimestamp = 0; // Not message packet / saved + mutable unsigned int hint_capability = 0; // Is this a video stream, for example + size_t hint_source_total = 0; // Number of tracks per frame to expect + int retry_count = 0; // Decode retry count unsigned int hint_peerid = 0; operator std::string() const; diff --git a/src/socket/socket_linux.cpp b/src/socket/socket_linux.cpp index 7989d7e18e219a334745bb24d61da0e059f3706f..d9b8b8f69de7b36eb319dc1cc0e57fd6eef2f55a 100644 --- a/src/socket/socket_linux.cpp +++ b/src/socket/socket_linux.cpp @@ -205,7 +205,14 @@ Socket ftl::net::internal::create_tcp_socket() { std::string ftl::net::internal::get_host(const SocketAddress& addr) { char hbuf[1024]; - int err = getnameinfo(reinterpret_cast<const sockaddr*>(&(addr.addr)), addr.len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD); + int err = getnameinfo( + reinterpret_cast<const sockaddr*>(&(addr.addr)), + addr.len, + hbuf, + sizeof(hbuf), + NULL, + 0, + NI_NAMEREQD); if (err == 0) { return std::string(hbuf); } else if (err == EAI_NONAME) return ftl::net::internal::get_ip(addr); else diff --git a/src/streams/filestream.cpp b/src/streams/filestream.cpp index db69504262b174b556d08a408c549de5531f1fdc..ff09e653a54dd324b8385c8865e74c54c930bca5 100644 --- a/src/streams/filestream.cpp +++ b/src/streams/filestream.cpp @@ -10,6 +10,7 @@ #include <utility> #include <limits> #include <algorithm> +#include <filesystem> #include <thread> #include <chrono> #include "filestream.hpp" @@ -478,6 +479,14 @@ bool File::run() { return true; } +bool File::_validateFilename() const { + std::filesystem::path file = std::filesystem::u8path(uri_.toFilePath()); + if (!std::filesystem::exists(file)) return true; + if (std::string(file.extension().u8string().c_str()) == ".ftl") return true; + // TODO(Nick): Could also check directory path + return false; +} + bool File::begin() { if (active_) return true; if (mode_ == Mode::Read) { @@ -498,6 +507,7 @@ bool File::begin() { run(); } else if (mode_ == Mode::Write) { if (!ostream_) ostream_ = new std::ofstream; + if (!_validateFilename()) return false; ostream_->open(uri_.toFilePath(), std::ifstream::out | std::ifstream::binary); if (!ostream_->good()) { diff --git a/src/streams/filestream.hpp b/src/streams/filestream.hpp index 269b866a7bc1a35ad16902e2f84432f114a5674b..89d2991d280cf1a991cb46610db9cda0a8061357 100644 --- a/src/streams/filestream.hpp +++ b/src/streams/filestream.hpp @@ -122,6 +122,7 @@ class File : public Stream { bool _open(); bool _checkFile(); + bool _validateFilename() const; /* Apply version patches etc... */ void _patchPackets(ftl::protocol::StreamPacket *spkt, ftl::protocol::DataPacket *pkt); diff --git a/test/filestream_unit.cpp b/test/filestream_unit.cpp index 2cb0f30d3c6ce5eee0ebe7bbc13f4db7625fa7fe..64ed59d8f2871b6d649e459a4e2a048b210d3761 100644 --- a/test/filestream_unit.cpp +++ b/test/filestream_unit.cpp @@ -1,5 +1,6 @@ #include "catch.hpp" +#include <fstream> #include <filesystem> #include <ftl/protocol/streams.hpp> #include <ftl/protocol.hpp> @@ -124,3 +125,13 @@ TEST_CASE("File write and read", "[stream]") { REQUIRE( channels[2] == Channel::kScreen ); } } + +TEST_CASE("File write fails for bad filename", "[stream]") { + std::string filename = (std::filesystem::temp_directory_path() / "badfile.exe").string(); + std::ofstream out(filename); + out << "something"; + out.close(); + auto writer = ftl::createStream(filename); + + REQUIRE(!writer->begin()); +}