Skip to content

Commit

Permalink
build: fix level 1 msvc warnings
Browse files Browse the repository at this point in the history
Level 1 warnings are considered "severe" and, unsurprisingly, turning
them on revealed some true bugs.

- file-buffer.hpp: What should have been a zero extend followed by a
  complement was instead a complement followed by a zero extend,
  inadvertently truncating the file seek offset to 32 bits.
- file-map.cpp: What should have been a nullptr check after
  CreateFileMapping() was instead a check for INVALID_HANDLE_VALUE. The
  warning pointing me toward this was not technically about the misuse
  but rather the redefinition of INVALID_HANDLE_VALUE in nall. I've
  removed this and any reference to INVALID_HANDLE_VALUE except for
  error checks immediately after calls to the few APIs that return it.
  • Loading branch information
invertego committed Nov 19, 2023
1 parent 92c1c2e commit 15094e4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion libco/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#endif

#if defined(_MSC_VER)
#pragma section(".text", execute, read)
#pragma section(".text")
#define section(name) __declspec(allocate(".text"))
#elif defined(__APPLE__)
#define section(name) __attribute__((section("__TEXT,__" #name)))
Expand Down
7 changes: 4 additions & 3 deletions nall/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ endif
# global compiler flags
ifeq ($(cl),true)
flags.c = -nologo -W0 -Fd$(object.path)/ -TC -std:c11
flags.cpp = -nologo -W0 -Fd$(object.path)/ -TP -std:c++17 -EHsc
options = -nologo $(if $(findstring clang,$(compiler)),-fuse-ld=lld) -link
flags.c = -TC -std:c11
flags.cpp = -TP -std:c++17 -EHsc
flags += -nologo -W1 -Fd$(object.path)/
options += -nologo $(if $(findstring clang,$(compiler)),-fuse-ld=lld) -link
else
flags.c = -x c -std=c11
flags.cpp = -x c++ -std=c++17
Expand Down
4 changes: 2 additions & 2 deletions nall/file-buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ struct file_buffer {

auto bufferSynchronize() -> void {
if(!fileHandle) return;
if(bufferOffset == (fileOffset & ~(buffer.size() - 1))) return;
if(bufferOffset == (fileOffset & ~u64(buffer.size() - 1))) return;

bufferFlush();
bufferOffset = fileOffset & ~(buffer.size() - 1);
bufferOffset = fileOffset & ~u64(buffer.size() - 1);
fseek(fileHandle, bufferOffset, SEEK_SET);
u64 length = bufferOffset + buffer.size() <= fileSize ? buffer.size() : fileSize & buffer.size() - 1;
if(length) (void)fread(buffer.data(), 1, length, fileHandle);
Expand Down
17 changes: 10 additions & 7 deletions nall/file-map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ NALL_HEADER_INLINE auto file_map::open(const string& filename, u32 mode_) -> boo

_file = CreateFileW(utf16_t(filename), desiredAccess, FILE_SHARE_READ, nullptr,
creationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
if(_file == INVALID_HANDLE_VALUE) return false;
if(_file == INVALID_HANDLE_VALUE) {
_file = nullptr;
return false;
}

_size = GetFileSize(_file, nullptr);

_map = CreateFileMapping(_file, nullptr, protection, 0, _size, nullptr);
if(_map == INVALID_HANDLE_VALUE) {
if(_map == nullptr) {
CloseHandle(_file);
_file = INVALID_HANDLE_VALUE;
_file = nullptr;
return false;
}

Expand All @@ -62,14 +65,14 @@ NALL_HEADER_INLINE auto file_map::close() -> void {
_data = nullptr;
}

if(_map != INVALID_HANDLE_VALUE) {
if(_map != nullptr) {
CloseHandle(_map);
_map = INVALID_HANDLE_VALUE;
_map = nullptr;
}

if(_file != INVALID_HANDLE_VALUE) {
if(_file != nullptr) {
CloseHandle(_file);
_file = INVALID_HANDLE_VALUE;
_file = nullptr;
}

_open = false;
Expand Down
8 changes: 4 additions & 4 deletions nall/file-map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct file_map {

#if defined(API_WINDOWS)

HANDLE _file = INVALID_HANDLE_VALUE;
HANDLE _map = INVALID_HANDLE_VALUE;
HANDLE _file = nullptr;
HANDLE _map = nullptr;

public:
auto operator=(file_map&& source) -> file_map& {
Expand All @@ -67,8 +67,8 @@ struct file_map {
source._open = false;
source._data = nullptr;
source._size = 0;
source._file = INVALID_HANDLE_VALUE;
source._map = INVALID_HANDLE_VALUE;
source._file = nullptr;
source._map = nullptr;

return *this;
}
Expand Down
2 changes: 2 additions & 0 deletions nall/intrinsics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace nall {
static constexpr bool GCC = 0;
static constexpr bool Microsoft = 1;
};
#pragma warning(disable:4804) //unsafe use of type 'bool' in operation
#pragma warning(disable:4805) //unsafe mix of type 'bool' and type 'type' in operation
#pragma warning(disable:4996) //libc "deprecation" warnings
#else
#error "unable to detect compiler"
Expand Down
4 changes: 0 additions & 4 deletions nall/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ namespace Math {
#define MSG_NOSIGNAL 0
#define PATH_MAX 260

#if !defined(INVALID_HANDLE_VALUE)
#define INVALID_HANDLE_VALUE ((HANDLE)-1)
#endif

typedef void* HANDLE;

inline auto access(const char* path, int amode) -> int { return _waccess(nall::utf16_t(path), amode); }
Expand Down

0 comments on commit 15094e4

Please sign in to comment.