Skip to content

Commit 95ffd43

Browse files
Fixes for fault about String::copy()
1 parent 1f5b289 commit 95ffd43

3 files changed

Lines changed: 59 additions & 39 deletions

File tree

client/mysql.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ static bool add_line(String &buffer,char *line,char *in_string)
952952
}
953953
if ((com=find_command(NullS,(char) inchar)))
954954
{
955-
const String tmp(line,(uint) (out-line));
955+
const String tmp(line,(uint) (out-line), system_charset_info);
956956
buffer.append(tmp);
957957
if ((*com->func)(&buffer,pos-1) > 0)
958958
return 1; // Quit
@@ -1709,7 +1709,7 @@ print_table_data(MYSQL_RES *result)
17091709
print_field_types(result);
17101710
mysql_field_seek(result,0);
17111711
}
1712-
separator.copy("+",1);
1712+
separator.copy("+",1,system_charset_info);
17131713
while ((field = mysql_fetch_field(result)))
17141714
{
17151715
uint length= column_names ? (uint) strlen(field->name) : 0;

client/sql_string.cc

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@ extern void sql_element_free(void *ptr);
4040
bool String::real_alloc(uint32 arg_length)
4141
{
4242
arg_length=ALIGN_SIZE(arg_length+1);
43+
str_length=0;
4344
if (Alloced_length < arg_length)
4445
{
4546
free();
4647
if (!(Ptr=(char*) my_malloc(arg_length,MYF(MY_WME))))
47-
{
48-
str_length=0;
4948
return TRUE;
50-
}
5149
Alloced_length=arg_length;
5250
alloced=1;
5351
}
5452
Ptr[0]=0;
55-
str_length=0;
5653
return FALSE;
5754
}
5855

@@ -94,36 +91,40 @@ bool String::realloc(uint32 alloc_length)
9491
return FALSE;
9592
}
9693

97-
bool String::set(longlong num)
94+
bool String::set(longlong num, CHARSET_INFO *cs)
9895
{
9996
if (alloc(21))
10097
return TRUE;
10198
str_length=(uint32) (longlong10_to_str(num,Ptr,-10)-Ptr);
99+
str_charset=cs;
102100
return FALSE;
103101
}
104102

105-
bool String::set(ulonglong num)
103+
bool String::set(ulonglong num, CHARSET_INFO *cs)
106104
{
107105
if (alloc(21))
108106
return TRUE;
109107
str_length=(uint32) (longlong10_to_str(num,Ptr,10)-Ptr);
108+
str_charset=cs;
110109
return FALSE;
111110
}
112111

113-
bool String::set(double num,uint decimals)
112+
bool String::set(double num,uint decimals, CHARSET_INFO *cs)
114113
{
115114
char buff[331];
115+
116+
str_charset=cs;
116117
if (decimals >= NOT_FIXED_DEC)
117118
{
118119
sprintf(buff,"%.14g",num); // Enough for a DATETIME
119-
return copy(buff, (uint32) strlen(buff));
120+
return copy(buff, (uint32) strlen(buff), my_charset_latin1);
120121
}
121122
#ifdef HAVE_FCONVERT
122123
int decpt,sign;
123124
char *pos,*to;
124125

125126
VOID(fconvert(num,(int) decimals,&decpt,&sign,buff+1));
126-
if (!isdigit(buff[1]))
127+
if (!my_isdigit(system_charset_info, buff[1]))
127128
{ // Nan or Inf
128129
pos=buff+1;
129130
if (sign)
@@ -181,7 +182,7 @@ bool String::set(double num,uint decimals)
181182
#else
182183
sprintf(buff,"%.*f",(int) decimals,num);
183184
#endif
184-
return copy(buff,(uint32) strlen(buff));
185+
return copy(buff,(uint32) strlen(buff), my_charset_latin1);
185186
#endif
186187
}
187188

@@ -203,16 +204,18 @@ bool String::copy(const String &str)
203204
str_length=str.str_length;
204205
bmove(Ptr,str.Ptr,str_length); // May be overlapping
205206
Ptr[str_length]=0;
207+
str_charset=str.str_charset;
206208
return FALSE;
207209
}
208210

209-
bool String::copy(const char *str,uint32 arg_length)
211+
bool String::copy(const char *str,uint32 arg_length, CHARSET_INFO *cs)
210212
{
211213
if (alloc(arg_length))
212214
return TRUE;
213215
if ((str_length=arg_length))
214216
memcpy(Ptr,str,arg_length);
215217
Ptr[arg_length]=0;
218+
str_charset=cs;
216219
return FALSE;
217220
}
218221

@@ -489,7 +492,7 @@ void String::qs_append(double d)
489492
void String::qs_append(double *d)
490493
{
491494
double ld;
492-
float8get(ld, d);
495+
float8get(ld, (char*) d);
493496
qs_append(ld);
494497
}
495498

@@ -523,12 +526,23 @@ int sortcmp(const String *x,const String *y)
523526
#endif /* USE_STRCOLL */
524527
x_len-=len; // For easy end space test
525528
y_len-=len;
526-
while (len--)
529+
if (x->str_charset->sort_order)
527530
{
528-
if (x->str_charset->sort_order[(uchar) *s++] !=
531+
while (len--)
532+
{
533+
if (x->str_charset->sort_order[(uchar) *s++] !=
529534
x->str_charset->sort_order[(uchar) *t++])
530-
return ((int) x->str_charset->sort_order[(uchar) s[-1]] -
531-
(int) x->str_charset->sort_order[(uchar) t[-1]]);
535+
return ((int) x->str_charset->sort_order[(uchar) s[-1]] -
536+
(int) x->str_charset->sort_order[(uchar) t[-1]]);
537+
}
538+
}
539+
else
540+
{
541+
while (len--)
542+
{
543+
if (*s++ != *t++)
544+
return ((int) s[-1] - (int) t[-1]);
545+
}
532546
}
533547
#ifndef CMP_ENDSPACE
534548
/* Don't compare end space in strings */
@@ -586,6 +600,7 @@ String *copy_if_not_alloced(String *to,String *from,uint32 from_length)
586600
return from; // Actually an error
587601
if ((to->str_length=min(from->str_length,from_length)))
588602
memcpy(to->Ptr,from->Ptr,to->str_length);
603+
to->str_charset=from->str_charset;
589604
return to;
590605
}
591606

@@ -658,7 +673,7 @@ int wild_case_compare(CHARSET_INFO *cs, const char *str,const char *str_end,
658673
{ // Found wild_many
659674
wildstr++;
660675
/* Remove any '%' and '_' from the wild search string */
661-
for ( ; wildstr != wildend ; wildstr++)
676+
for (; wildstr != wildend ; wildstr++)
662677
{
663678
if (*wildstr == wild_many)
664679
continue;
@@ -787,7 +802,7 @@ int wild_compare(const char *str,const char *str_end,
787802
{ // Found wild_many
788803
wildstr++;
789804
/* Remove any '%' and '_' from the wild search string */
790-
for ( ; wildstr != wildend ; wildstr++)
805+
for (; wildstr != wildend ; wildstr++)
791806
{
792807
if (*wildstr == wild_many)
793808
continue;

client/sql_string.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@ class String
4646
String(uint32 length_arg)
4747
{
4848
alloced=0; Alloced_length=0; (void) real_alloc(length_arg);
49-
str_charset=default_charset_info;
49+
str_charset=default_charset_info;
5050
}
51-
String(const char *str)
51+
String(const char *str, CHARSET_INFO *cs)
5252
{
5353
Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;
54-
str_charset=default_charset_info;
54+
str_charset=cs;
5555
}
56-
String(const char *str,uint32 len)
56+
String(const char *str,uint32 len, CHARSET_INFO *cs)
5757
{
5858
Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;
59-
str_charset=default_charset_info;
59+
str_charset=cs;
6060
}
61-
String(char *str,uint32 len)
61+
String(char *str,uint32 len, CHARSET_INFO *cs)
6262
{
6363
Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;
64-
str_charset=default_charset_info;
64+
str_charset=cs;
6565
}
6666
String(const String &str)
6767
{
@@ -74,6 +74,7 @@ class String
7474
{ sql_element_free(ptr_arg); }
7575
~String() { free(); }
7676

77+
inline void set_charset(CHARSET_INFO *charset) { str_charset=charset; }
7778
inline CHARSET_INFO *charset() const { return str_charset; }
7879
inline uint32 length() const { return str_length;}
7980
inline uint32 alloced_length() const { return Alloced_length;}
@@ -102,28 +103,31 @@ class String
102103
Alloced_length=str.Alloced_length-offset;
103104
else
104105
Alloced_length=0;
106+
str_charset=str.str_charset;
105107
}
106-
inline void set(char *str,uint32 arg_length)
108+
inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
107109
{
108110
free();
109111
Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
112+
str_charset=cs;
110113
}
111-
inline void set(const char *str,uint32 arg_length)
114+
inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs)
112115
{
113116
free();
114117
Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0;
118+
str_charset=cs;
115119
}
116-
inline void set_quick(char *str,uint32 arg_length)
120+
inline void set_quick(char *str,uint32 arg_length, CHARSET_INFO *cs)
117121
{
118122
if (!alloced)
119123
{
120124
Ptr=(char*) str; str_length=Alloced_length=arg_length;
121125
}
126+
str_charset=cs;
122127
}
123-
bool set(longlong num);
124-
/* bool set(long num); */
125-
bool set(ulonglong num);
126-
bool set(double num,uint decimals=2);
128+
bool set(longlong num, CHARSET_INFO *cs);
129+
bool set(ulonglong num, CHARSET_INFO *cs);
130+
bool set(double num,uint decimals, CHARSET_INFO *cs);
127131
inline void free()
128132
{
129133
if (alloced)
@@ -174,7 +178,7 @@ class String
174178

175179
bool copy(); // Alloc string if not alloced
176180
bool copy(const String &s); // Allocate new string
177-
bool copy(const char *s,uint32 arg_length); // Allocate new string
181+
bool copy(const char *s,uint32 arg_length, CHARSET_INFO *cs); // Allocate new string
178182
bool append(const String &s);
179183
bool append(const char *s,uint32 arg_length=0);
180184
bool append(IO_CACHE* file, uint32 arg_length);
@@ -208,16 +212,17 @@ class String
208212
uint32 numchars();
209213
int charpos(int i,uint32 offset=0);
210214

211-
// added by Holyfoot for "geometry" needs
212215
int reserve(uint32 space_needed)
213216
{
214217
return realloc(str_length + space_needed);
215218
}
216219
int reserve(uint32 space_needed, uint32 grow_by);
217220

218-
// these append operations do NOT check alloced memory
219-
// q_*** methods writes values of parameters itself
220-
// qs_*** methods writes string representation of value
221+
/*
222+
The following append operations do NOT check alloced memory
223+
q_*** methods writes values of parameters itself
224+
qs_*** methods writes string representation of value
225+
*/
221226
void q_append(const char &c)
222227
{
223228
Ptr[str_length++] = c;

0 commit comments

Comments
 (0)