From 83acd5f17e67f6bbe586a061a4317815f176e196 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 2 Oct 2018 20:21:08 +0200 Subject: One less local variable in marshalls --- core/io/marshalls.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index ec430d41a9..6338cee39d 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -850,17 +850,16 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::INT: { - int64_t val = p_variant; if (flags & ENCODE_FLAG_64) { //64 bits if (buf) { - encode_uint64(val, buf); + encode_uint64(p_variant.operator int64_t(), buf); } r_len += 8; } else { if (buf) { - encode_uint32(int32_t(val), buf); + encode_uint32(p_variant.operator int32_t(), buf); } r_len += 4; -- cgit v1.2.3 From 5393e7310d16656f2a84e0df589c54f9ccfbcd59 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 2 Oct 2018 20:42:57 +0200 Subject: Avoid possible overflow in OS_Unix readlink Also fix [-Wunused-result] --- drivers/unix/os_unix.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 6c70934bc6..7ff27be501 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -487,9 +487,11 @@ String OS_Unix::get_executable_path() const { //fix for running from a symlink char buf[256]; memset(buf, 0, 256); - readlink("/proc/self/exe", buf, sizeof(buf)); + ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf)); String b; - b.parse_utf8(buf); + if (len > 0) { + b.parse_utf8(buf, len); + } if (b == "") { WARN_PRINT("Couldn't get executable path from /proc/self/exe, using argv[0]"); return OS::get_executable_path(); -- cgit v1.2.3 From 12124d2d445878b785160e01ae931973eb1ab4d9 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 2 Oct 2018 21:07:32 +0200 Subject: Safer getcwd in DirAccess Fix [-Wunused-result] --- drivers/unix/dir_access_unix.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index 48b4369b7e..929e67faa9 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -309,7 +309,7 @@ Error DirAccessUnix::change_dir(String p_dir) { // prev_dir is the directory we are changing out of String prev_dir; char real_current_dir_name[2048]; - getcwd(real_current_dir_name, 2048); + ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == NULL, ERR_BUG); if (prev_dir.parse_utf8(real_current_dir_name)) prev_dir = real_current_dir_name; //no utf8, maybe latin? @@ -330,7 +330,7 @@ Error DirAccessUnix::change_dir(String p_dir) { // the directory exists, so set current_dir to try_dir current_dir = try_dir; - chdir(prev_dir.utf8().get_data()); + ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG); return OK; } @@ -405,7 +405,7 @@ DirAccessUnix::DirAccessUnix() { // set current directory to an absolute path of the current directory char real_current_dir_name[2048]; - getcwd(real_current_dir_name, 2048); + ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == NULL); if (current_dir.parse_utf8(real_current_dir_name)) current_dir = real_current_dir_name; -- cgit v1.2.3 From 67177586d362a123f65d8725a2369f7cf85da66b Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Tue, 2 Oct 2018 21:15:14 +0200 Subject: Fix potentially unininitialized pointer write. --- core/os/rw_lock.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h index 8d1029723b..4333d9a016 100644 --- a/core/os/rw_lock.h +++ b/core/os/rw_lock.h @@ -57,9 +57,7 @@ class RWLockRead { public: RWLockRead(const RWLock *p_lock) { - if (p_lock) { - lock = const_cast(p_lock); - } + lock = const_cast(p_lock); if (lock) lock->read_lock(); } ~RWLockRead() { -- cgit v1.2.3 From f8020bc9766e7641fb3e53101bd6d85494e7fa41 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 6 Oct 2018 07:14:28 +0200 Subject: Check getcwd return in X11 platform main. --- platform/x11/godot_x11.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/platform/x11/godot_x11.cpp b/platform/x11/godot_x11.cpp index 3241cbcbf9..21148f8e86 100644 --- a/platform/x11/godot_x11.cpp +++ b/platform/x11/godot_x11.cpp @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { setlocale(LC_CTYPE, ""); char *cwd = (char *)malloc(PATH_MAX); - getcwd(cwd, PATH_MAX); + char *ret = getcwd(cwd, PATH_MAX); Error err = Main::setup(argv[0], argc - 1, &argv[1]); if (err != OK) { @@ -55,7 +55,8 @@ int main(int argc, char *argv[]) { os.run(); // it is actually the OS that decides how to run Main::cleanup(); - chdir(cwd); + if (ret) + chdir(cwd); free(cwd); return os.get_exit_code(); -- cgit v1.2.3 From d65afb2c7474f4f7b19aacc74dd3956ee491c60b Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 6 Oct 2018 08:35:43 +0200 Subject: Fix LWSClient connect_to_host string termination. Coming from strncpy might get you a non-NULL terminated buffer. The solution, if you accept trunction, is to give one less byte to strncpy and manually set the last char in the buffer to '\0'. If the source string is shorter, than the buffer is padded with '\0' automatically. --- modules/websocket/lws_client.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/websocket/lws_client.cpp b/modules/websocket/lws_client.cpp index cd814760e6..b3e5f6ffab 100644 --- a/modules/websocket/lws_client.cpp +++ b/modules/websocket/lws_client.cpp @@ -80,9 +80,12 @@ Error LWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, char hbuf[1024]; char pbuf[2048]; String addr_str = (String)addr; - strncpy(abuf, addr_str.ascii().get_data(), 1024); - strncpy(hbuf, p_host.utf8().get_data(), 1024); - strncpy(pbuf, p_path.utf8().get_data(), 2048); + strncpy(abuf, addr_str.ascii().get_data(), 1023); + abuf[1023] = '\0'; + strncpy(hbuf, p_host.utf8().get_data(), 1023); + hbuf[1023] = '\0'; + strncpy(pbuf, p_path.utf8().get_data(), 2047); + pbuf[2047] = '\0'; i.context = context; if (p_protocols.size() > 0) -- cgit v1.2.3