From: Rico Tzschichholz Date: Sun, 1 Nov 2020 20:16:49 +0000 (+0100) Subject: vala: Use DataType.compatible() to check for string concatenation X-Git-Tag: 0.40.25~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a758bdbabf79ed753785ec278cb1a68d16a352a;p=thirdparty%2Fvala.git vala: Use DataType.compatible() to check for string concatenation Make the checks match the ones performed by the code-generator to prevent invalid c-code to be created. See https://gitlab.gnome.org/GNOME/vala/issues/1100 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 18560b63b..9d428d5ff 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -32,6 +32,7 @@ TESTS = \ basic-types/arrays-fixed-assignment.vala \ basic-types/array-uint8-uchar-compat.vala \ basic-types/pointers.vala \ + basic-types/pointers-arithmetic.vala \ basic-types/sizeof.vala \ basic-types/garray.vala \ basic-types/glists.vala \ @@ -879,6 +880,7 @@ NON_NULL_TESTS = \ nullability/member-access-nullable-instance.test \ nullability/method-parameter-invalid-convert.test \ nullability/method-return-invalid-convert.test \ + nullability/string-concat.test \ $(NULL) LINUX_TESTS = \ diff --git a/tests/basic-types/pointers-arithmetic.vala b/tests/basic-types/pointers-arithmetic.vala new file mode 100644 index 000000000..79437f087 --- /dev/null +++ b/tests/basic-types/pointers-arithmetic.vala @@ -0,0 +1,22 @@ +void test_chars () { + char* s = "foo"; + char* begin = s; + char* end = begin + 2; + + assert (begin[0] == 'f'); + assert (end[0] == 'o'); +} + +void test_strings () { + string s = "foo"; + string* begin = s; + string* end = begin + s.length - 1; + + assert (((char*) begin)[0] == 'f'); + assert (((char*) end)[0] == 'o'); +} + +void main () { + test_chars (); + test_strings (); +} diff --git a/tests/nullability/string-concat.test b/tests/nullability/string-concat.test new file mode 100644 index 000000000..5cc78efc6 --- /dev/null +++ b/tests/nullability/string-concat.test @@ -0,0 +1,6 @@ +Invalid Code + +void main () { + string? foo = null; + string bar = foo + "bar"; +} diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala index 783cdc055..421ebf165 100644 --- a/vala/valabinaryexpression.vala +++ b/vala/valabinaryexpression.vala @@ -343,11 +343,11 @@ public class Vala.BinaryExpression : Expression { right.target_type = right.value_type.copy (); right.target_type.value_owned = false; - if (left.value_type.data_type == context.analyzer.string_type.data_type - && operator == BinaryOperator.PLUS) { + if (operator == BinaryOperator.PLUS && !(left.value_type is PointerType) + && left.value_type.compatible (context.analyzer.string_type)) { // string concatenation - if (right.value_type == null || right.value_type.data_type != context.analyzer.string_type.data_type) { + if (right.value_type == null || !right.value_type.compatible (context.analyzer.string_type)) { error = true; Report.error (source_reference, "Operands must be strings"); return false;