Skip to content

Commit d468baf

Browse files
fix: all signed compare warnings
1 parent dd9bf97 commit d468baf

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ if(BUFFERSTREAM_BUILD_TESTS)
2727
FetchContent_MakeAvailable(googletest)
2828
enable_testing()
2929

30+
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
31+
3032
add_executable(${BUFFERSTREAM_TEST_NAME}
3133
"${CMAKE_CURRENT_SOURCE_DIR}/test/BufferStream.cpp")
3234

3335
target_link_libraries(${BUFFERSTREAM_TEST_NAME} PUBLIC
3436
gtest_main ${PROJECT_NAME})
3537

38+
if (MSVC)
39+
target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE
40+
/W4)
41+
else()
42+
target_compile_options(${BUFFERSTREAM_TEST_NAME} PRIVATE
43+
-Wall -Wextra -Wpedantic)
44+
endif()
45+
3646
include(GoogleTest)
3747
gtest_discover_tests(${BUFFERSTREAM_TEST_NAME})
3848
endif()

include/BufferStream.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class BufferStream {
128128
this->bufferPos = offset;
129129
break;
130130
case std::ios::cur:
131-
if (this->useExceptions && (std::cmp_greater(this->bufferPos + offset, this->bufferLen) || this->bufferPos + offset < 0)) {
131+
if (this->useExceptions && (std::cmp_greater(this->bufferPos + offset, this->bufferLen) || static_cast<int64_t>(this->bufferPos) + offset < 0)) {
132132
throw std::overflow_error{BUFFERSTREAM_OVERFLOW_READ_ERROR_MESSAGE};
133133
}
134134
this->bufferPos += offset;
@@ -284,7 +284,7 @@ class BufferStream {
284284
std::memcpy(obj, this->buffer + this->bufferPos, N);
285285
this->bufferPos += N;
286286
} else {
287-
for (int i = 0; i < N; i++) {
287+
for (std::uint64_t i = 0; i < N; i++) {
288288
this->read(obj[i]);
289289
}
290290
}
@@ -306,7 +306,7 @@ class BufferStream {
306306
std::memcpy(this->buffer + this->bufferPos, obj, N);
307307
this->bufferPos += N;
308308
} else {
309-
for (int i = 0; i < N; i++) {
309+
for (std::uint64_t i = 0; i < N; i++) {
310310
this->write(obj[i]);
311311
}
312312
}
@@ -324,8 +324,8 @@ class BufferStream {
324324
throw std::overflow_error{BUFFERSTREAM_OVERFLOW_READ_ERROR_MESSAGE};
325325
}
326326

327-
for (int i = 0; i < M; i++) {
328-
for (int j = 0; j < N; j++) {
327+
for (std::uint64_t i = 0; i < M; i++) {
328+
for (std::uint64_t j = 0; j < N; j++) {
329329
this->read(obj[i][j]);
330330
}
331331
}
@@ -343,8 +343,8 @@ class BufferStream {
343343
throw std::overflow_error{BUFFERSTREAM_OVERFLOW_WRITE_ERROR_MESSAGE};
344344
}
345345

346-
for (int i = 0; i < M; i++) {
347-
for (int j = 0; j < N; j++) {
346+
for (std::uint64_t i = 0; i < M; i++) {
347+
for (std::uint64_t j = 0; j < N; j++) {
348348
this->write(obj[i][j]);
349349
}
350350
}
@@ -366,7 +366,7 @@ class BufferStream {
366366
std::memcpy(obj.data(), this->buffer + this->bufferPos, N);
367367
this->bufferPos += N;
368368
} else {
369-
for (int i = 0; i < N; i++) {
369+
for (std::uint64_t i = 0; i < N; i++) {
370370
this->read(obj[i]);
371371
}
372372
}
@@ -388,7 +388,7 @@ class BufferStream {
388388
std::memcpy(this->buffer + this->bufferPos, obj.data(), N);
389389
this->bufferPos += N;
390390
} else {
391-
for (int i = 0; i < N; i++) {
391+
for (std::uint64_t i = 0; i < N; i++) {
392392
this->write(obj[i]);
393393
}
394394
}
@@ -422,7 +422,7 @@ class BufferStream {
422422
}) {
423423
obj.reserve(n);
424424
}
425-
for (int i = 0; i < n; i++) {
425+
for (std::uint64_t i = 0; i < n; i++) {
426426
obj.push_back(this->read<typename T::value_type>());
427427
}
428428
}
@@ -449,7 +449,7 @@ class BufferStream {
449449
std::memcpy(this->buffer + this->bufferPos, obj.data(), obj.size());
450450
this->bufferPos += obj.size();
451451
} else {
452-
for (int i = 0; i < obj.size(); i++) {
452+
for (decltype(obj.size()) i = 0; i < obj.size(); i++) {
453453
this->write(obj[i]);
454454
}
455455
}
@@ -517,7 +517,7 @@ class BufferStream {
517517
std::memcpy(obj.data(), this->buffer + this->bufferPos, obj.size());
518518
this->bufferPos += obj.size();
519519
} else {
520-
for (int i = 0; i < obj.size(); i++) {
520+
for (decltype(obj.size()) i = 0; i < obj.size(); i++) {
521521
obj[i] = this->read<T>();
522522
}
523523
}
@@ -548,7 +548,7 @@ class BufferStream {
548548
std::memcpy(obj.data(), this->buffer + this->bufferPos, n);
549549
this->bufferPos += n;
550550
} else {
551-
for (int i = 0; i < n; i++) {
551+
for (std::uint64_t i = 0; i < n; i++) {
552552
obj[i] = this->read<T>();
553553
}
554554
}
@@ -570,7 +570,7 @@ class BufferStream {
570570
std::memcpy(this->buffer + this->bufferPos, obj.data(), obj.size());
571571
this->bufferPos += obj.size();
572572
} else {
573-
for (int i = 0; i < obj.size(); i++) {
573+
for (decltype(obj.size()) i = 0; i < obj.size(); i++) {
574574
this->write(obj[i]);
575575
}
576576
}
@@ -644,7 +644,7 @@ class BufferStream {
644644
}
645645

646646
obj.reserve(n);
647-
for (int i = 0; i < n; i++) {
647+
for (std::uint64_t i = 0; i < n; i++) {
648648
char temp = this->read<char>();
649649
if (temp == '\0' && stopOnNullTerminator) {
650650
// Read the required number of characters and exit
@@ -723,7 +723,7 @@ class BufferStream {
723723
}
724724
return this->buffer[offset];
725725
case std::ios::cur:
726-
if (this->useExceptions && (std::cmp_greater(this->bufferPos + offset, this->bufferLen) || this->bufferPos + offset < 0)) {
726+
if (this->useExceptions && (std::cmp_greater(this->bufferPos + offset, this->bufferLen) || static_cast<int64_t>(this->bufferPos) + offset < 0)) {
727727
throw std::overflow_error{BUFFERSTREAM_OVERFLOW_READ_ERROR_MESSAGE};
728728
}
729729
return this->buffer[this->bufferPos + offset];
@@ -860,7 +860,7 @@ class BufferStream {
860860
dest.bytes[k] = source.bytes[sizeof(T) - k - 1];
861861
}
862862
*t = dest.t;
863-
};
863+
}
864864

865865
protected:
866866
std::byte* buffer;

0 commit comments

Comments
 (0)