aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/handle.cpp10
-rw-r--r--src/path.cpp7
-rw-r--r--src/process.cpp6
-rw-r--r--test/unit_tests/shared/console.hpp30
-rw-r--r--test/unit_tests/worker/worker.cpp4
5 files changed, 38 insertions, 19 deletions
diff --git a/src/handle.cpp b/src/handle.cpp
index 4678519..ba1e4b5 100644
--- a/src/handle.cpp
+++ b/src/handle.cpp
@@ -14,6 +14,7 @@
#include <cassert>
#include <cstddef>
+#include <limits>
#include <sstream>
#include <stdexcept>
#include <utility>
@@ -83,7 +84,10 @@ bool Handle::read_chunk(Buffer& buffer) const {
buffer.resize(max_chunk_size);
DWORD nb_read = 0;
- const auto ret = ::ReadFile(m_impl.get(), buffer.data(), buffer.size(), &nb_read, NULL);
+ if (buffer.size() > std::numeric_limits<DWORD>::max())
+ throw std::range_error{"Read buffer is too large"};
+ const auto ret =
+ ::ReadFile(m_impl.get(), buffer.data(), static_cast<DWORD>(buffer.size()), &nb_read, NULL);
buffer.resize(nb_read);
@@ -121,7 +125,9 @@ Buffer Handle::read() const {
void Handle::write(const void* data, std::size_t nb) const {
DWORD nb_written = 0;
- const auto ret = ::WriteFile(m_impl.get(), data, nb, &nb_written, NULL);
+ if (nb > std::numeric_limits<DWORD>::max())
+ throw std::range_error{"Write buffer is too large"};
+ const auto ret = ::WriteFile(m_impl.get(), data, static_cast<DWORD>(nb), &nb_written, NULL);
if (!ret) {
throw error::windows(GetLastError(), "WriteFile");
diff --git a/src/path.cpp b/src/path.cpp
index 3df5918..7848df2 100644
--- a/src/path.cpp
+++ b/src/path.cpp
@@ -9,6 +9,8 @@
#include <windows.h>
+#include <limits>
+#include <stdexcept>
#include <string>
#include <vector>
@@ -23,7 +25,10 @@ std::wstring do_canonicalize(const std::wstring& path) {
buffer.resize(init_buffer_size);
while (true) {
- const auto nch = ::GetFullPathNameW(path.c_str(), buffer.size(), buffer.data(), NULL);
+ if (buffer.size() > std::numeric_limits<DWORD>::max())
+ throw std::range_error{"Path buffer is too large"};
+ const auto nch = ::GetFullPathNameW(
+ path.c_str(), static_cast<DWORD>(buffer.size()), buffer.data(), NULL);
if (nch == 0) {
throw error::windows(GetLastError(), "GetFullPathNameW");
diff --git a/src/process.cpp b/src/process.cpp
index bac8bb8..2be7f0d 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -19,6 +19,7 @@
#include <cstddef>
#include <cstring>
+#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
@@ -305,7 +306,10 @@ std::string Process::get_exe_path() {
while (true) {
SetLastError(ERROR_SUCCESS);
- const auto nch = ::GetModuleFileNameW(NULL, buffer.data(), buffer.size());
+ if (buffer.size() > std::numeric_limits<DWORD>::max())
+ throw std::range_error{"Path buffer is too large"};
+ const auto nch =
+ ::GetModuleFileNameW(NULL, buffer.data(), static_cast<DWORD>(buffer.size()));
if (nch == 0) {
throw error::windows(GetLastError(), "GetModuleFileNameW");
diff --git a/test/unit_tests/shared/console.hpp b/test/unit_tests/shared/console.hpp
index 274c232..cd9cccf 100644
--- a/test/unit_tests/shared/console.hpp
+++ b/test/unit_tests/shared/console.hpp
@@ -14,8 +14,10 @@
#include <windows.h>
#include <cstddef>
+#include <cstdint>
#include <cstring>
#include <sstream>
+#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
@@ -30,13 +32,13 @@ public:
Buffer(winapi::Handle&& handle) : m_handle(std::move(handle)), m_info(get_info(m_handle)) {}
- std::size_t get_columns() const { return m_info.dwSize.X; }
+ int16_t get_columns() const { return m_info.dwSize.X; }
- std::size_t get_lines() const { return m_info.dwSize.Y; }
+ int16_t get_lines() const { return m_info.dwSize.Y; }
- std::size_t get_cursor_column() const { return m_info.dwCursorPosition.X; }
+ int16_t get_cursor_column() const { return m_info.dwCursorPosition.X; }
- std::size_t get_cursor_line() const { return m_info.dwCursorPosition.Y; }
+ int16_t get_cursor_line() const { return m_info.dwCursorPosition.Y; }
void update() { m_info = get_info(m_handle); }
@@ -65,17 +67,17 @@ public:
* I also don't know how it interacts with tab characters '\t', encodings,
* etc. It sucks, don't use it.
*/
- std::vector<std::string> read_lines(int top, int bottom) const {
- if (top < 0) {
+ std::vector<std::string> read_lines(int16_t top, int16_t bottom) const {
+ if (top < 0)
top = get_cursor_line() + top;
- }
- if (bottom < 0) {
+ if (bottom < 0)
bottom = get_cursor_line() + bottom;
- }
+ if (top < 0 || bottom < 0)
+ throw std::range_error{"Invalid console line"};
if (top > bottom) {
std::swap(top, bottom);
}
- int numof_lines = bottom - top + 1;
+ int16_t numof_lines = bottom - top + 1;
COORD buffer_size;
buffer_size.X = get_columns();
@@ -111,13 +113,15 @@ public:
return result;
}
- std::vector<std::string> read_last_lines(int numof_lines = 1) const {
- return read_lines(-numof_lines, -1);
+ std::vector<std::string> read_last_lines(std::size_t numof_lines = 1) const {
+ if (numof_lines < 1 || numof_lines > INT16_MAX)
+ throw std::range_error{"Invalid number of lines"};
+ return read_lines(-static_cast<int16_t>(numof_lines), -1);
}
std::string read_last_line() const { return read_lines(-1, -1)[0]; }
- std::string read_line(int n) const { return read_lines(n, n)[0]; }
+ std::string read_line(int16_t n) const { return read_lines(n, n)[0]; }
private:
static Info get_info(const winapi::Handle& handle) {
diff --git a/test/unit_tests/worker/worker.cpp b/test/unit_tests/worker/worker.cpp
index 9e48deb..093bba8 100644
--- a/test/unit_tests/worker/worker.cpp
+++ b/test/unit_tests/worker/worker.cpp
@@ -43,7 +43,7 @@ bool is_window_visible() {
winapi::Handle write_to(winapi::Handle dest, const std::string& msg) {
try {
dest.write(msg + "\r\n");
- } catch (const std::exception& e) {
+ } catch (const std::exception&) {
return winapi::Handle{};
}
return dest;
@@ -111,7 +111,7 @@ int loop() {
} // namespace
-int main(int argc, char* argv[]) {
+int main() {
int ec = loop();
std::this_thread::sleep_for(std::chrono::milliseconds{1000});
return ec;