Skip to content

Commit

Permalink
Fix invalid string to int cast yields 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanCheetah committed Nov 23, 2024
1 parent 49339fd commit effd55a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
16 changes: 16 additions & 0 deletions core/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
#include "core/print_string.h"
#include "core/resource.h"
#include "core/variant_parser.h"
#include "modules/regex/regex.h"
#include "scene/gui/control.h"
#include "scene/main/node.h"

RegEx number("^((-?\\d+)(\\.\\d+)?|true|false)$");

String Variant::get_type_name(Variant::Type p_type) {
switch (p_type) {
case NIL: {
Expand Down Expand Up @@ -601,6 +604,19 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type
return false;
}

bool Variant::can_convert(Type p_type_to) const {
// If this is a string variant and we are converting to an integer, check if the contents of the string
// make sense as an integer
if (type == Type::STRING && p_type_to == Type::INT) {
// Use regular expression to validate the string
Ref<RegExMatch> match = number.search(operator String());
return match != NULL;
}

// Do other type checks
return Variant::can_convert(type, p_type_to);
}

bool Variant::deep_equal(const Variant &p_variant, int p_recursion_count) const {
ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, true, "Max recursion reached");

Expand Down
1 change: 1 addition & 0 deletions core/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class Variant {
static String get_type_name(Variant::Type p_type);
static bool can_convert(Type p_type_from, Type p_type_to);
static bool can_convert_strict(Type p_type_from, Type p_type_to);
bool can_convert(Type p_type_to) const;

bool is_ref() const;
_FORCE_INLINE_ bool is_num() const { return type == INT || type == REAL; };
Expand Down
6 changes: 3 additions & 3 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct _VariantCall {
if (tptr[i] == Variant::NIL || tptr[i] == p_args[i]->type) {
continue; // all good
}
if (!Variant::can_convert(p_args[i]->type, tptr[i])) {
if (!p_args[i]->can_convert(tptr[i])) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = i;
r_error.expected = tptr[i];
Expand Down Expand Up @@ -1335,7 +1335,7 @@ Variant Variant::construct(const Variant::Type p_type, const Variant **p_args, i

} else if (p_argcount == 1 && p_args[0]->type == p_type) {
return *p_args[0]; //copy construct
} else if (p_argcount == 1 && (!p_strict || Variant::can_convert(p_args[0]->type, p_type))) {
} else if (p_argcount == 1 && (!p_strict || p_args[0]->can_convert(p_type))) {
//near match construct

switch (p_type) {
Expand Down Expand Up @@ -1418,7 +1418,7 @@ Variant Variant::construct(const Variant::Type p_type, const Variant **p_args, i

//validate parameters
for (int i = 0; i < cd.arg_count; i++) {
if (!Variant::can_convert(p_args[i]->type, cd.arg_types[i])) {
if (!p_args[i]->can_convert(cd.arg_types[i])) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; //no such constructor
r_error.argument = i;
r_error.expected = cd.arg_types[i];
Expand Down
8 changes: 4 additions & 4 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class AnimationTrackKeyEdit : public Object {

if (t != args[idx].get_type()) {
Variant::CallError err;
if (Variant::can_convert(args[idx].get_type(), t)) {
if (args[idx].can_convert(t)) {
Variant old = args[idx];
Variant *ptrs[1] = { &old };
args.write[idx] = Variant::construct(t, (const Variant **)ptrs, 1, err);
Expand Down Expand Up @@ -844,7 +844,7 @@ class AnimationMultiTrackKeyEdit : public Object {

if (t != args[idx].get_type()) {
Variant::CallError err;
if (Variant::can_convert(args[idx].get_type(), t)) {
if (args[idx].can_convert(t)) {
Variant old = args[idx];
Variant *ptrs[1] = { &old };
args.write[idx] = Variant::construct(t, (const Variant **)ptrs, 1, err);
Expand Down Expand Up @@ -2423,7 +2423,7 @@ bool AnimationTrackEdit::_is_value_key_valid(const Variant &p_key_value, Variant
r_valid_type = obj->get_static_property_type_indexed(leftover_path, &prop_exists);
}

return (!prop_exists || Variant::can_convert(p_key_value.get_type(), r_valid_type));
return (!prop_exists || p_key_value.can_convert(r_valid_type));
}

Ref<Texture> AnimationTrackEdit::_get_key_type_icon() const {
Expand Down Expand Up @@ -5681,7 +5681,7 @@ void AnimationTrackEditor::_cleanup_animation(Ref<Animation> p_animation) {
for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
Variant v = p_animation->track_get_key_value(i, j);

if (!Variant::can_convert(v.get_type(), valid_type)) {
if (!v.can_convert(valid_type)) {
p_animation->track_remove_key(i, j);
j--;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5127,7 +5127,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
if (cn->value.get_type() != Variant::NIL) {
if (member._export.type != Variant::NIL && cn->value.get_type() != member._export.type) {
if (!Variant::can_convert(cn->value.get_type(), member._export.type)) {
if (!cn->value.can_convert(member._export.type)) {
_set_error("Can't convert the provided value to the export type.");
return;
} else if (!member.data_type.has_type) {
Expand Down
2 changes: 1 addition & 1 deletion platform/android/api/jni_singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class JNISingleton : public Object {
bool call_error = !E || E->get().argtypes.size() != p_argcount;
if (!call_error) {
for (int i = 0; i < p_argcount; i++) {
if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
if (!p_args[i]->can_convert(E->get().argtypes[i])) {
call_error = true;
break;
}
Expand Down

0 comments on commit effd55a

Please sign in to comment.