Skip to content

Commit

Permalink
Merge pull request godotengine#100386 from hpvb/core-ubsan
Browse files Browse the repository at this point in the history
Core: Fix UBSAN reports
  • Loading branch information
akien-mga committed Dec 18, 2024
2 parents d458253 + 240f510 commit 851d8e4
Show file tree
Hide file tree
Showing 40 changed files with 370 additions and 245 deletions.
8 changes: 4 additions & 4 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<S

if (!p_custom_features.is_empty()) {
// Store how many properties are saved, add one for custom features, which must always go first.
file->store_32(count + 1);
file->store_32(uint32_t(count + 1));
String key = CoreStringName(_custom_features);
file->store_pascal_string(key);

Expand All @@ -911,12 +911,12 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<S

err = encode_variant(p_custom_features, buff.ptrw(), len, false);
ERR_FAIL_COND_V(err != OK, err);
file->store_32(len);
file->store_32(uint32_t(len));
file->store_buffer(buff.ptr(), buff.size());

} else {
// Store how many properties are saved.
file->store_32(count);
file->store_32(uint32_t(count));
}

for (const KeyValue<String, List<String>> &E : p_props) {
Expand All @@ -943,7 +943,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<S

err = encode_variant(value, buff.ptrw(), len, true);
ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
file->store_32(len);
file->store_32(uint32_t(len));
file->store_buffer(buff.ptr(), buff.size());
}
}
Expand Down
6 changes: 2 additions & 4 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ class ProjectSettings : public Object {
typedef HashMap<String, Variant> CustomMap;
static const String PROJECT_DATA_DIR_NAME_SUFFIX;

enum {
// Properties that are not for built in values begin from this value, so builtin ones are displayed first.
NO_BUILTIN_ORDER_BASE = 1 << 16
};
// Properties that are not for built in values begin from this value, so builtin ones are displayed first.
constexpr static const int32_t NO_BUILTIN_ORDER_BASE = 1 << 16;

#ifdef TOOLS_ENABLED
const static PackedStringArray get_required_features();
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/aes_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AESContext : public RefCounted {
GDCLASS(AESContext, RefCounted);

public:
enum Mode {
enum Mode : int32_t {
MODE_ECB_ENCRYPT,
MODE_ECB_DECRYPT,
MODE_CBC_ENCRYPT,
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/hashing_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class HashingContext : public RefCounted {
GDCLASS(HashingContext, RefCounted);

public:
enum HashType {
enum HashType : int32_t {
HASH_MD5,
HASH_SHA1,
HASH_SHA256
Expand Down
4 changes: 4 additions & 0 deletions core/input/input_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#ifndef INPUT_ENUMS_H
#define INPUT_ENUMS_H

#include "core/error/error_macros.h"

enum class HatDir {
UP = 0,
RIGHT = 1,
Expand Down Expand Up @@ -131,6 +133,8 @@ enum class MouseButtonMask {
};

inline MouseButtonMask mouse_button_to_mask(MouseButton button) {
ERR_FAIL_COND_V(button == MouseButton::NONE, MouseButtonMask::NONE);

return MouseButtonMask(1 << ((int)button - 1));
}

Expand Down
2 changes: 1 addition & 1 deletion core/io/compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Compression {
static int zstd_window_log_size;
static int gzip_chunk;

enum Mode {
enum Mode : int32_t {
MODE_FASTLZ,
MODE_DEFLATE,
MODE_ZSTD,
Expand Down
2 changes: 1 addition & 1 deletion core/io/dir_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DirAccess : public RefCounted {
GDCLASS(DirAccess, RefCounted);

public:
enum AccessType {
enum AccessType : int32_t {
ACCESS_RESOURCES,
ACCESS_USERDATA,
ACCESS_FILESYSTEM,
Expand Down
10 changes: 5 additions & 5 deletions core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,15 @@ double FileAccess::get_double() const {
String FileAccess::get_token() const {
CharString token;

char32_t c = get_8();
uint8_t c = get_8();

while (!eof_reached()) {
if (c <= ' ') {
if (token.length()) {
break;
}
} else {
token += c;
token += char(c);
}
c = get_8();
}
Expand Down Expand Up @@ -448,14 +448,14 @@ class CharBuffer {
String FileAccess::get_line() const {
CharBuffer line;

char32_t c = get_8();
uint8_t c = get_8();

while (!eof_reached()) {
if (c == '\n' || c == '\0') {
line.push_back(0);
return String::utf8(line.get_data());
} else if (c != '\r') {
line.push_back(c);
line.push_back(char(c));
}

c = get_8();
Expand Down Expand Up @@ -786,7 +786,7 @@ bool FileAccess::store_var(const Variant &p_var, bool p_full_objects) {
err = encode_variant(p_var, &w[0], len, p_full_objects);
ERR_FAIL_COND_V_MSG(err != OK, false, "Error when trying to encode Variant.");

return store_32(len) && store_buffer(buff);
return store_32(uint32_t(len)) && store_buffer(buff);
}

Vector<uint8_t> FileAccess::get_file_as_bytes(const String &p_path, Error *r_error) {
Expand Down
8 changes: 4 additions & 4 deletions core/io/file_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ class FileAccess : public RefCounted {
GDCLASS(FileAccess, RefCounted);

public:
enum AccessType {
enum AccessType : int32_t {
ACCESS_RESOURCES,
ACCESS_USERDATA,
ACCESS_FILESYSTEM,
ACCESS_PIPE,
ACCESS_MAX
};

enum ModeFlags {
enum ModeFlags : int32_t {
READ = 1,
WRITE = 2,
READ_WRITE = 3,
WRITE_READ = 7,
};

enum UnixPermissionFlags {
enum UnixPermissionFlags : int32_t {
UNIX_EXECUTE_OTHER = 0x001,
UNIX_WRITE_OTHER = 0x002,
UNIX_READ_OTHER = 0x004,
Expand All @@ -76,7 +76,7 @@ class FileAccess : public RefCounted {
UNIX_SET_USER_ID = 0x800,
};

enum CompressionMode {
enum CompressionMode : int32_t {
COMPRESSION_FASTLZ = Compression::MODE_FASTLZ,
COMPRESSION_DEFLATE = Compression::MODE_DEFLATE,
COMPRESSION_ZSTD = Compression::MODE_ZSTD,
Expand Down
8 changes: 5 additions & 3 deletions core/io/file_access_compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void FileAccessCompressed::_close() {
f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //write header 4
f->store_32(cmode); //write compression mode 4
f->store_32(block_size); //write block size 4
f->store_32(write_max); //max amount of data written 4
f->store_32(uint32_t(write_max)); //max amount of data written 4
uint32_t bc = (write_max / block_size) + 1;

for (uint32_t i = 0; i < bc; i++) {
Expand All @@ -147,7 +147,7 @@ void FileAccessCompressed::_close() {

f->seek(16); //ok write block sizes
for (uint32_t i = 0; i < bc; i++) {
f->store_32(block_sizes[i]);
f->store_32(uint32_t(block_sizes[i]));
}
f->seek_end();
f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
Expand Down Expand Up @@ -310,7 +310,9 @@ bool FileAccessCompressed::store_buffer(const uint8_t *p_src, uint64_t p_length)
write_ptr = buffer.ptrw();
}

memcpy(write_ptr + write_pos, p_src, p_length);
if (p_length) {
memcpy(write_ptr + write_pos, p_src, p_length);
}

write_pos += p_length;
return true;
Expand Down
16 changes: 14 additions & 2 deletions core/io/file_access_encrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ bool FileAccessEncrypted::eof_reached() const {
}

uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");

if (!p_length) {
return 0;
}

ERR_FAIL_NULL_V(p_dst, -1);

uint64_t to_copy = MIN(p_length, get_length() - pos);

memcpy(p_dst, data.ptr() + pos, to_copy);
pos += to_copy;

Expand All @@ -230,14 +236,20 @@ Error FileAccessEncrypted::get_error() const {

bool FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_COND_V_MSG(!writing, false, "File has not been opened in write mode.");
ERR_FAIL_COND_V(!p_src && p_length > 0, false);

if (!p_length) {
return true;
}

ERR_FAIL_NULL_V(p_src, false);

if (pos + p_length >= get_length()) {
ERR_FAIL_COND_V(data.resize(pos + p_length) != OK, false);
}

memcpy(data.ptrw() + pos, p_src, p_length);
pos += p_length;

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion core/io/file_access_encrypted.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

class FileAccessEncrypted : public FileAccess {
public:
enum Mode {
enum Mode : int32_t {
MODE_READ,
MODE_WRITE_AES256,
MODE_MAX
Expand Down
12 changes: 10 additions & 2 deletions core/io/file_access_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ bool FileAccessMemory::eof_reached() const {
}

uint64_t FileAccessMemory::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
if (!p_length) {
return 0;
}

ERR_FAIL_NULL_V(p_dst, -1);
ERR_FAIL_NULL_V(data, -1);

uint64_t left = length - pos;
Expand All @@ -148,7 +152,11 @@ void FileAccessMemory::flush() {
}

bool FileAccessMemory::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_COND_V(!p_src && p_length > 0, false);
if (!p_length) {
return true;
}

ERR_FAIL_NULL_V(p_src, false);

uint64_t left = length - pos;
uint64_t write = MIN(p_length, left);
Expand Down
2 changes: 1 addition & 1 deletion core/io/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Image : public Resource {
MAX_PIXELS = 268435456 // 16384 ^ 2
};

enum Format {
enum Format : int32_t {
FORMAT_L8, // Luminance
FORMAT_LA8, // Luminance-Alpha
FORMAT_R8,
Expand Down
18 changes: 11 additions & 7 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_
return itos(p_var);
case Variant::FLOAT: {
double num = p_var;
if (p_full_precision) {
// Store unreliable digits (17) instead of just reliable
// digits (14) so that the value can be decoded exactly.
return String::num(num, 17 - (int)floor(log10(num)));
} else {
// Store only reliable digits (14) by default.
return String::num(num, 14 - (int)floor(log10(num)));

// Only for exactly 0. If we have approximately 0 let the user decide how much
// precision they want.
if (num == double(0)) {
return String("0.0");
}

double magnitude = log10(Math::abs(num));
int total_digits = p_full_precision ? 17 : 14;
int precision = MAX(1, total_digits - (int)Math::floor(magnitude));

return String::num(num, precision);
}
case Variant::PACKED_INT32_ARRAY:
case Variant::PACKED_INT64_ARRAY:
Expand Down
8 changes: 4 additions & 4 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
case Variant::INT: {
if (header & HEADER_DATA_FLAG_64) {
ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA);
int64_t val = decode_uint64(buf);
int64_t val = int64_t(decode_uint64(buf));
r_variant = val;
if (r_len) {
(*r_len) += 8;
}

} else {
ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
int32_t val = decode_uint32(buf);
int32_t val = int32_t(decode_uint32(buf));
r_variant = val;
if (r_len) {
(*r_len) += 4;
Expand Down Expand Up @@ -1450,13 +1450,13 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
if (header & HEADER_DATA_FLAG_64) {
// 64 bits.
if (buf) {
encode_uint64(p_variant.operator int64_t(), buf);
encode_uint64(p_variant.operator uint64_t(), buf);
}

r_len += 8;
} else {
if (buf) {
encode_uint32(p_variant.operator int32_t(), buf);
encode_uint32(p_variant.operator uint32_t(), buf);
}

r_len += 4;
Expand Down
4 changes: 2 additions & 2 deletions core/io/net_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class NetSocket : public RefCounted {
public:
static NetSocket *create();

enum PollType {
enum PollType : int32_t {
POLL_TYPE_IN,
POLL_TYPE_OUT,
POLL_TYPE_IN_OUT
};

enum Type {
enum Type : int32_t {
TYPE_NONE,
TYPE_TCP,
TYPE_UDP,
Expand Down
6 changes: 3 additions & 3 deletions core/io/pck_packer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Error PCKPacker::flush(bool p_verbose) {
}

// write the index
file->store_32(files.size());
file->store_32(uint32_t(files.size()));

Ref<FileAccessEncrypted> fae;
Ref<FileAccess> fhead = file;
Expand All @@ -201,7 +201,7 @@ Error PCKPacker::flush(bool p_verbose) {
int string_len = files[i].path.utf8().length();
int pad = _get_pad(4, string_len);

fhead->store_32(string_len + pad);
fhead->store_32(uint32_t(string_len + pad));
fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len);
for (int j = 0; j < pad; j++) {
fhead->store_8(0);
Expand Down Expand Up @@ -231,7 +231,7 @@ Error PCKPacker::flush(bool p_verbose) {
file->store_8(0);
}

int64_t file_base = file->get_position();
uint64_t file_base = file->get_position();
file->seek(file_base_ofs);
file->store_64(file_base); // update files base
file->seek(file_base);
Expand Down
Loading

0 comments on commit 851d8e4

Please sign in to comment.