Skip to content

Commit

Permalink
MDEV-29646: sformat('Num [{:20}]', 42) gives incorrect result in view
Browse files Browse the repository at this point in the history
The problem is that sformat does not assign the enough space for the
result string. The result string is allocated with the max_length of
argument, but the correst max_length should be based on the format
string.

The patch fixes the problem by using MAX_BLOB_WIDTH to assign length
  • Loading branch information
Weijun-H authored and grooverdan committed May 4, 2023
1 parent 7d96742 commit f288d42
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
25 changes: 24 additions & 1 deletion mysql-test/main/func_sformat.result
Expand Up @@ -434,7 +434,7 @@ create table t1 as select sformat(_ucs2 x'003D007B007D003D', _ucs2 x'04420435044
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`x` varchar(8) CHARACTER SET ucs2 COLLATE ucs2_general_ci DEFAULT NULL
`x` longtext CHARACTER SET ucs2 COLLATE ucs2_general_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
drop table t1;
set names latin1;
Expand Down Expand Up @@ -468,3 +468,26 @@ set names latin1;
#
# End of 10.7 tests
#
#
# Start of 10.8 tests
#
#
# MDEV-29646 sformat('Num [{:20}]', 42) gives incorrect result in view
#
create view v1 as select sformat('Num [{:20}]', 42);
select * from v1;
sformat('Num [{:20}]', 42)
Num [ 42]
drop view v1;
create view v1 as SELECT sformat('Square root of [{:d}] is [{:.20}]', 2, sqrt(2));
select * from v1;
sformat('Square root of [{:d}] is [{:.20}]', 2, sqrt(2))
Square root of [2] is [1.4142135623730951455]
drop view v1;
create table t1 (a text, b int, c text);
insert t1 values ('[{} -> {}]', 10, '{}'), ('[{:20} <- {}]', 1, '{:30}');
select sformat(a,b,c) from t1;
sformat(a,b,c)
[10 -> {}]
[ 1 <- {:30}]
drop table t1;
21 changes: 21 additions & 0 deletions mysql-test/main/func_sformat.test
Expand Up @@ -253,3 +253,24 @@ set names latin1;
echo #;
echo # End of 10.7 tests;
echo #;

echo #;
echo # Start of 10.8 tests;
echo #;

echo #;
echo # MDEV-29646 sformat('Num [{:20}]', 42) gives incorrect result in view;
echo #;

create view v1 as select sformat('Num [{:20}]', 42);
select * from v1;
drop view v1;

create view v1 as SELECT sformat('Square root of [{:d}] is [{:.20}]', 2, sqrt(2));
select * from v1;
drop view v1;

create table t1 (a text, b int, c text);
insert t1 values ('[{} -> {}]', 10, '{}'), ('[{:20} <- {}]', 1, '{:30}');
select sformat(a,b,c) from t1;
drop table t1;
2 changes: 1 addition & 1 deletion sql/item_strfunc.cc
Expand Up @@ -1348,13 +1348,13 @@ bool Item_func_sformat::fix_length_and_dec()

for (uint i=0 ; i < arg_count ; i++)
{
char_length+= args[i]->max_char_length();
if (args[i]->result_type() == STRING_RESULT &&
Type_std_attributes::agg_item_set_converter(c, func_name_cstring(),
args+i, 1, flags, 1))
return TRUE;
}

char_length= MAX_BLOB_WIDTH;
fix_char_length_ulonglong(char_length);
return FALSE;
}
Expand Down

0 comments on commit f288d42

Please sign in to comment.