diff --git a/client/mysqltest.cc b/client/mysqltest.cc index c945f20a240..d1b26363d4d 100644 --- a/client/mysqltest.cc +++ b/client/mysqltest.cc @@ -227,8 +227,9 @@ typedef struct int str_val_len; int int_val; int alloced_len; - int int_dirty; /* do not update string if int is updated until first read */ - int alloced; + bool int_dirty; /* do not update string if int is updated until first read */ + bool is_int; + bool alloced; } VAR; /*Perl/shell-like variable registers */ @@ -1954,6 +1955,21 @@ static void var_free(void *v) C_MODE_END +void var_set_int(VAR *v, const char *str) +{ + char *endptr; + /* Initially assume not a number */ + v->int_val= 0; + v->is_int= false; + v->int_dirty= false; + if (!str) return; + + v->int_val = (int) strtol(str, &endptr, 10); + /* It is an int if strtol consumed something up to end/space/tab */ + if (endptr > str && (!*endptr || *endptr == ' ' || *endptr == '\t')) + v->is_int= true; +} + VAR *var_init(VAR *v, const char *name, int name_len, const char *val, int val_len) @@ -1988,11 +2004,10 @@ VAR *var_init(VAR *v, const char *name, int name_len, const char *val, memcpy(tmp_var->str_val, val, val_len); tmp_var->str_val[val_len]= 0; } + var_set_int(tmp_var, val); tmp_var->name_len = name_len; tmp_var->str_val_len = val_len; tmp_var->alloced_len = val_alloc_len; - tmp_var->int_val = (val) ? atoi(val) : 0; - tmp_var->int_dirty = 0; return tmp_var; } @@ -2053,7 +2068,7 @@ VAR* var_get(const char *var_name, const char **var_name_end, my_bool raw, if (!raw && v->int_dirty) { sprintf(v->str_val, "%d", v->int_val); - v->int_dirty = 0; + v->int_dirty= false; v->str_val_len = strlen(v->str_val); } if (var_name_end) @@ -2115,7 +2130,7 @@ void var_set(const char *var_name, const char *var_name_end, if (v->int_dirty) { sprintf(v->str_val, "%d", v->int_val); - v->int_dirty= 0; + v->int_dirty=false; v->str_val_len= strlen(v->str_val); } /* setenv() expects \0-terminated strings */ @@ -2421,6 +2436,7 @@ void var_set_query_get_value(struct st_command *command, VAR *var) void var_copy(VAR *dest, VAR *src) { dest->int_val= src->int_val; + dest->is_int= src->is_int; dest->int_dirty= src->int_dirty; /* Alloc/realloc data for str_val in dest */ @@ -2504,9 +2520,7 @@ void eval_expr(VAR *v, const char *p, const char **p_end) v->str_val_len = new_val_len; memcpy(v->str_val, p, new_val_len); v->str_val[new_val_len] = 0; - v->int_val=atoi(p); - DBUG_PRINT("info", ("atoi on '%s', returns: %d", p, v->int_val)); - v->int_dirty=0; + var_set_int(v, p); } DBUG_VOID_RETURN; } @@ -2853,6 +2867,8 @@ int do_modify_var(struct st_command *command, die("The argument to %.*s must be a variable (start with $)", command->first_word_len, command->query); v= var_get(p, &p, 1, 0); + if (! v->is_int) + die("Cannot perform inc/dec on a non-numeric value"); switch (op) { case DO_DEC: v->int_val--; @@ -2864,7 +2880,7 @@ int do_modify_var(struct st_command *command, die("Invalid operator to do_modify_var"); break; } - v->int_dirty= 1; + v->int_dirty= true; command->last_argument= (char*)++p; return 0; } diff --git a/mysql-test/r/mysqltest.result b/mysql-test/r/mysqltest.result index 650cc069b7f..d7597599f02 100644 --- a/mysql-test/r/mysqltest.result +++ b/mysql-test/r/mysqltest.result @@ -369,23 +369,24 @@ mysqltest: At line 1: Missing required argument 'sleep_delay' to command 'real_s mysqltest: At line 1: Invalid argument to sleep "abc" mysqltest: At line 1: Invalid argument to real_sleep "abc" 1 -2 101 -hej -1 +-99 mysqltest: At line 1: Missing argument to inc mysqltest: At line 1: The argument to inc must be a variable (start with $) +mysqltest: At line 1: Cannot perform inc/dec on a non-numeric value mysqltest: At line 1: End of line junk detected: "1000" -4 -4 +mysqltest: At line 1: Cannot perform inc/dec on a non-numeric value +mysqltest: At line 1: Cannot perform inc/dec on a non-numeric value +-96 +-96 -1 --2 99 -hej --1 mysqltest: At line 1: Missing argument to dec mysqltest: At line 1: The argument to dec must be a variable (start with $) +mysqltest: At line 1: Cannot perform inc/dec on a non-numeric value mysqltest: At line 1: End of line junk detected: "1000" +mysqltest: At line 1: Cannot perform inc/dec on a non-numeric value +mysqltest: At line 1: Cannot perform inc/dec on a non-numeric value mysqltest: At line 1: Missing arguments to system, nothing to do! mysqltest: At line 1: Missing arguments to system, nothing to do! system command 'NonExistsinfComamdn 2> /dev/null' failed diff --git a/mysql-test/t/mysqltest.test b/mysql-test/t/mysqltest.test index c054b163c0c..45dbb4a5c8d 100644 --- a/mysql-test/t/mysqltest.test +++ b/mysql-test/t/mysqltest.test @@ -1006,16 +1006,13 @@ EOF # ---------------------------------------------------------------------------- # Test inc # ---------------------------------------------------------------------------- -inc $i; -echo $i; +let $i= 0; inc $i; echo $i; let $i=100; inc $i; echo $i; - -let $i=hej; -echo $i; +let $i= -100; inc $i; echo $i; @@ -1024,7 +1021,13 @@ echo $i; --error 1 --exec echo "inc i;" | $MYSQL_TEST 2>&1 --error 1 +--exec echo "inc \$i;" | $MYSQL_TEST 2>&1 +--error 1 --exec echo "let \$i=100; inc \$i 1000; echo \$i;" | $MYSQL_TEST 2>&1 +--error 1 +--exec echo "let \$i=text; inc \$i; echo \$i;" | $MYSQL_TEST 2>&1 +--error 1 +--exec echo "let \$i=10cc; inc \$i; echo \$i;" | $MYSQL_TEST 2>&1 inc $i; inc $i; inc $i; --echo $i echo $i; @@ -1034,25 +1037,25 @@ echo $i; # Test dec # ---------------------------------------------------------------------------- -dec $d; -echo $d; +let $d= 0; dec $d; echo $d; let $d=100; dec $d; echo $d; -let $d=hej; -echo $d; -dec $d; -echo $d; - --error 1 --exec echo "dec;" | $MYSQL_TEST 2>&1 --error 1 --exec echo "dec i;" | $MYSQL_TEST 2>&1 --error 1 +--exec echo "dec \$i;" | $MYSQL_TEST 2>&1 +--error 1 --exec echo "let \$i=100; dec \$i 1000; echo \$i;" | $MYSQL_TEST 2>&1 +--error 1 +--exec echo "let \$i=text; dec \$i; echo \$i;" | $MYSQL_TEST 2>&1 +--error 1 +--exec echo "let \$i=10cc; dec \$i; echo \$i;" | $MYSQL_TEST 2>&1 # ----------------------------------------------------------------------------