[vala/staging: 1/7] Check printf arguments of object creation expressions



commit 67ecc0e8278f348bbc1e9afad45449c75c0326ef
Author: Jürg Billeter <j bitron ch>
Date:   Tue Nov 1 19:10:36 2016 +0100

    Check printf arguments of object creation expressions

 vala/valamethodcall.vala               |    2 +
 vala/valaobjectcreationexpression.vala |   37 ++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 0 deletions(-)
---
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index 3d5835c..d3889eb 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -384,6 +384,8 @@ public class Vala.MethodCall : Expression {
                        }
                }
 
+               // FIXME partial code duplication in ObjectCreationExpression.check
+
                Expression last_arg = null;
 
                var args = get_argument_list ();
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index 0699503..2007a6c 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -361,6 +361,10 @@ public class Vala.ObjectCreationExpression : Expression {
                                context.analyzer.current_method.yield_count++;
                        }
 
+                       // FIXME partial code duplication of MethodCall.check
+
+                       Expression last_arg = null;
+
                        var args = get_argument_list ();
                        Iterator<Expression> arg_it = args.iterator ();
                        foreach (Parameter param in m.get_parameters ()) {
@@ -374,6 +378,39 @@ public class Vala.ObjectCreationExpression : Expression {
                                        /* store expected type for callback parameters */
                                        arg.formal_target_type = param.variable_type;
                                        arg.target_type = arg.formal_target_type.get_actual_type (value_type, 
null, this);
+
+                                       last_arg = arg;
+                               }
+                       }
+
+
+                       // printf arguments
+                       if (m.printf_format) {
+                               StringLiteral format_literal = null;
+                               if (last_arg != null) {
+                                       // use last argument as format string
+                                       format_literal = last_arg as StringLiteral;
+                                       if (format_literal == null && args.size == m.get_parameters ().size - 
1) {
+                                               // insert "%s" to avoid issues with embedded %
+                                               format_literal = new StringLiteral ("\"%s\"");
+                                               format_literal.target_type = 
context.analyzer.string_type.copy ();
+                                               argument_list.insert (args.size - 1, format_literal);
+
+                                               // recreate iterator and skip to right position
+                                               arg_it = argument_list.iterator ();
+                                               foreach (Parameter param in m.get_parameters ()) {
+                                                       if (param.ellipsis) {
+                                                               break;
+                                                       }
+                                                       arg_it.next ();
+                                               }
+                                       }
+                               }
+                               if (format_literal != null) {
+                                       string format = format_literal.eval ();
+                                       if (!context.analyzer.check_print_format (format, arg_it, 
source_reference)) {
+                                               return false;
+                                       }
                                }
                        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]