Skip to content

Commit

Permalink
Merge pull request #62477 from lyuma/packedbytearray
Browse files Browse the repository at this point in the history
Prevent out-of-bounds write in array conversion; avoid logspam on empty arrays.
  • Loading branch information
akien-mga authored Jul 1, 2022
2 parents afdae67 + 33fd7c6 commit 8819226
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
3 changes: 3 additions & 0 deletions core/templates/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class Vector {

Vector<uint8_t> to_byte_array() const {
Vector<uint8_t> ret;
if (is_empty()) {
return ret;
}
ret.resize(size() * sizeof(T));
memcpy(ret.ptrw(), ptr(), sizeof(T) * size());
return ret;
Expand Down
32 changes: 24 additions & 8 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,40 +753,56 @@ struct _VariantCall {
static PackedInt32Array func_PackedByteArray_decode_s32_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedInt32Array dest;
ERR_FAIL_COND_V_MSG(size < sizeof(int32_t), dest, "Size didn't match array of size int32_t, maybe you are trying to convert to the wrong type?");
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(int32_t), dest, "PackedByteArray size must be a multiple of 4 (size of 32-bit integer) to convert to PackedInt32Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(int32_t));
memcpy(dest.ptrw(), r, size);
ERR_FAIL_COND_V(dest.size() == 0, dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(int32_t));
return dest;
}

static PackedInt64Array func_PackedByteArray_decode_s64_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedInt64Array dest;
ERR_FAIL_COND_V_MSG(size < sizeof(int64_t), dest, "Size didn't match array of size int64_t, maybe you are trying to convert to the wrong type?");
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(int64_t), dest, "PackedByteArray size must be a multiple of 8 (size of 64-bit integer) to convert to PackedInt64Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(int64_t));
memcpy(dest.ptrw(), r, size);
ERR_FAIL_COND_V(dest.size() == 0, dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(int64_t));
return dest;
}

static PackedFloat32Array func_PackedByteArray_decode_float_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedFloat32Array dest;
ERR_FAIL_COND_V_MSG(size < sizeof(float), dest, "Size didn't match array of size float, maybe you are trying to convert to the wrong type?");
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(float), dest, "PackedByteArray size must be a multiple of 4 (size of 32-bit float) to convert to PackedFloat32Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(float));
memcpy(dest.ptrw(), r, size);
ERR_FAIL_COND_V(dest.size() == 0, dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(float));
return dest;
}

static PackedFloat64Array func_PackedByteArray_decode_double_array(PackedByteArray *p_instance) {
uint64_t size = p_instance->size();
PackedFloat64Array dest;
ERR_FAIL_COND_V_MSG(size < sizeof(double), dest, "Size didn't match array of size double, maybe you are trying to convert to the wrong type?");
if (size == 0) {
return dest;
}
ERR_FAIL_COND_V_MSG(size % sizeof(double), dest, "PackedByteArray size must be a multiple of 8 (size of 64-bit double) to convert to PackedFloat64Array.");
const uint8_t *r = p_instance->ptr();
dest.resize(size / sizeof(double));
memcpy(dest.ptrw(), r, size);
ERR_FAIL_COND_V(dest.size() == 0, dest); // Avoid UB in case resize failed.
memcpy(dest.ptrw(), r, dest.size() * sizeof(double));
return dest;
}

Expand Down
10 changes: 5 additions & 5 deletions doc/classes/PackedByteArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,31 +409,31 @@
<return type="PackedFloat32Array" />
<description>
Returns a copy of the data converted to a [PackedFloat32Array], where each block of 4 bytes has been converted to a 32-bit float (C++ [code]float[/code]).
The size of the new array will be [code]byte_array.size() / 4[/code].
The size of the input array must be a multiple of 4 (size of 32-bit float). The size of the new array will be [code]byte_array.size() / 4[/code].
If the original data can't be converted to 32-bit floats, the resulting data is undefined.
</description>
</method>
<method name="to_float64_array" qualifiers="const">
<return type="PackedFloat64Array" />
<description>
Returns a copy of the data converted to a [PackedFloat64Array], where each block of 8 bytes has been converted to a 64-bit float (C++ [code]double[/code], Godot [float]).
The size of the new array will be [code]byte_array.size() / 8[/code].
The size of the input array must be a multiple of 8 (size of 64-bit double). The size of the new array will be [code]byte_array.size() / 8[/code].
If the original data can't be converted to 64-bit floats, the resulting data is undefined.
</description>
</method>
<method name="to_int32_array" qualifiers="const">
<return type="PackedInt32Array" />
<description>
Returns a copy of the data converted to a [PackedInt32Array], where each block of 4 bytes has been converted to a signed 32-bit integer (C++ [code]int32_t[/code]).
The size of the new array will be [code]byte_array.size() / 4[/code].
The size of the input array must be a multiple of 4 (size of 32-bit integer). The size of the new array will be [code]byte_array.size() / 4[/code].
If the original data can't be converted to signed 32-bit integers, the resulting data is undefined.
</description>
</method>
<method name="to_int64_array" qualifiers="const">
<return type="PackedInt64Array" />
<description>
Returns a copy of the data converted to a [PackedInt64Array], where each block of 4 bytes has been converted to a signed 64-bit integer (C++ [code]int64_t[/code], Godot [int]).
The size of the new array will be [code]byte_array.size() / 8[/code].
Returns a copy of the data converted to a [PackedInt64Array], where each block of 8 bytes has been converted to a signed 64-bit integer (C++ [code]int64_t[/code], Godot [int]).
The size of the input array must be a multiple of 8 (size of 64-bit integer). The size of the new array will be [code]byte_array.size() / 8[/code].
If the original data can't be converted to signed 64-bit integers, the resulting data is undefined.
</description>
</method>
Expand Down

0 comments on commit 8819226

Please sign in to comment.