#At file:///home/bm136801/my/ifexpr-55/ based on revid:bjorn.munch@stripped
3093 Bjorn Munch 2010-09-27
Bug #57036 Add checks in mysqltest that variables treated as ints are in fact ints
Adds boolean flag is_int and a separete function to check for int value
Added tests to mysqltest.test
modified:
client/mysqltest.cc
mysql-test/r/mysqltest.result
mysql-test/t/mysqltest.test
=== modified file 'client/mysqltest.cc'
--- a/client/mysqltest.cc 2010-09-21 09:18:53 +0000
+++ b/client/mysqltest.cc 2010-09-27 12:36:16 +0000
@@ -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,
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
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
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_c
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, co
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 *com
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 *com
die("Invalid operator to do_modify_var");
break;
}
- v->int_dirty= 1;
+ v->int_dirty= true;
command->last_argument= (char*)++p;
return 0;
}
=== modified file 'mysql-test/r/mysqltest.result'
--- a/mysql-test/r/mysqltest.result 2010-09-20 08:08:32 +0000
+++ b/mysql-test/r/mysqltest.result 2010-09-27 12:36:16 +0000
@@ -369,23 +369,24 @@ mysqltest: At line 1: Missing required a
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
=== modified file 'mysql-test/t/mysqltest.test'
--- a/mysql-test/t/mysqltest.test 2010-09-21 09:18:53 +0000
+++ b/mysql-test/t/mysqltest.test 2010-09-27 12:36:16 +0000
@@ -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
# ----------------------------------------------------------------------------
Attachment: [text/bzr-bundle] bzr/bjorn.munch@oracle.com-20100927123616-2dz7eapdp7ndo3j5.bundle
| Thread |
|---|
| • bzr commit into mysql-5.5-mtr branch (bjorn.munch:3093) Bug#57036 | Bjorn Munch | 27 Sep |