Skip to content

Commit 3db76c9

Browse files
committed
- Fix MDEV-13925: Actually this fixes SELECT queries when
the WHERE clause have single quote. modified: storage/connect/ha_connect.cc - Use Windows VirtualAlloc and VirtualFree for the Sarea workspace modified: storage/connect/global.h modified: storage/connect/ha_connect.cc modified: storage/connect/jsonudf.cpp modified: storage/connect/plgdbutl.cpp modified: storage/connect/plugutil.cpp modified: storage/connect/user_connect.cc
1 parent 74ffcbc commit 3db76c9

File tree

6 files changed

+94
-60
lines changed

6 files changed

+94
-60
lines changed

storage/connect/global.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ DllExport int PlugExit(PGLOBAL); // Plug global termination
217217
DllExport LPSTR PlugRemoveType(LPSTR, LPCSTR);
218218
DllExport LPCSTR PlugSetPath(LPSTR to, LPCSTR prefix, LPCSTR name, LPCSTR dir);
219219
DllExport BOOL PlugIsAbsolutePath(LPCSTR path);
220-
DllExport void *PlugAllocMem(PGLOBAL, uint);
220+
DllExport bool AllocSarea(PGLOBAL, uint);
221+
DllExport void FreeSarea(PGLOBAL);
221222
DllExport BOOL PlugSubSet(PGLOBAL, void *, uint);
222223
DllExport void *PlugSubAlloc(PGLOBAL, void *, size_t);
223224
DllExport char *PlugDup(PGLOBAL g, const char *str);

storage/connect/ha_connect.cc

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,7 +3009,9 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, const Item *cond)
30093009
return NULL;
30103010

30113011
if (!x) {
3012+
const char *p;
30123013
char *s = (ishav) ? havg : body;
3014+
uint j, k, n;
30133015

30143016
// Append the value to the filter
30153017
switch (args[i]->field_type()) {
@@ -3065,16 +3067,38 @@ PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, const Item *cond)
30653067
strcat(s, "'}");
30663068
break;
30673069
default:
3068-
strcat(s, "'");
3069-
strncat(s, res->ptr(), res->length());
3070-
strcat(s, "'");
3071-
} // endswitch field type
3070+
j = strlen(s);
3071+
s[j++] = '\'';
3072+
p = res->ptr();
3073+
n = res->length();
3074+
3075+
for (k = 0; k < n; k++) {
3076+
if (p[k] == '\'')
3077+
s[j++] = '\'';
3078+
3079+
s[j++] = p[k];
3080+
} // endfor k
3081+
3082+
s[j++] = '\'';
3083+
s[j] = 0;
3084+
} // endswitch field type
30723085

30733086
} else {
3074-
strcat(s, "'");
3075-
strncat(s, res->ptr(), res->length());
3076-
strcat(s, "'");
3077-
} // endif tty
3087+
j = strlen(s);
3088+
s[j++] = '\'';
3089+
p = res->ptr();
3090+
n = res->length();
3091+
3092+
for (k = 0; k < n; k++) {
3093+
if (p[k] == '\'')
3094+
s[j++] = '\'';
3095+
3096+
s[j++] = p[k];
3097+
} // endfor k
3098+
3099+
s[j++] = '\'';
3100+
s[j] = 0;
3101+
} // endif tty
30783102

30793103
break;
30803104
default:

storage/connect/jsonudf.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,23 +1505,16 @@ static my_bool CheckMemory(PGLOBAL g, UDF_INIT *initid, UDF_ARGS *args, uint n,
15051505
ml += g->More;
15061506

15071507
if (ml > g->Sarea_Size) {
1508-
#if !defined(DEVELOPMENT)
1509-
if (trace)
1510-
#endif
1511-
htrc("Freeing Sarea at %p size=%d\n", g->Sarea, g->Sarea_Size);
1512-
1513-
free(g->Sarea);
1508+
FreeSarea(g);
15141509

1515-
if (!(g->Sarea = PlugAllocMem(g, ml))) {
1510+
if (AllocSarea(g, ml)) {
15161511
char errmsg[MAX_STR];
15171512

15181513
sprintf(errmsg, MSG(WORK_AREA), g->Message);
15191514
strcpy(g->Message, errmsg);
1520-
g->Sarea_Size = 0;
15211515
return true;
1522-
} // endif Alloc
1516+
} // endif SareaAlloc
15231517

1524-
g->Sarea_Size = ml;
15251518
g->Createas = 0;
15261519
g->Xchk = NULL;
15271520
initid->max_length = rl;

storage/connect/plgdbutl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ PDBUSER PlgMakeUser(PGLOBAL g)
334334
{
335335
PDBUSER dbuserp;
336336

337-
if (!(dbuserp = (PDBUSER)PlugAllocMem(g, (uint)sizeof(DBUSERBLK)))) {
337+
if (!(dbuserp = (PDBUSER)malloc(sizeof(DBUSERBLK)))) {
338338
sprintf(g->Message, MSG(MALLOC_ERROR), "PlgMakeUser");
339339
return NULL;
340340
} // endif dbuserp

storage/connect/plugutil.cpp

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ PGLOBAL PlugInit(LPCSTR Language, uint worksize)
138138

139139
if (trace > 1)
140140
htrc("PlugInit: Language='%s'\n",
141-
((!Language) ? "Null" : (char*)Language));
141+
((!Language) ? "Null" : (char*)Language));
142142

143143
try {
144144
g = new GLOBAL;
@@ -160,13 +160,11 @@ PGLOBAL PlugInit(LPCSTR Language, uint worksize)
160160
/*******************************************************************/
161161
/* Allocate the main work segment. */
162162
/*******************************************************************/
163-
if (worksize && !(g->Sarea = PlugAllocMem(g, worksize))) {
163+
if (worksize && AllocSarea(g, worksize)) {
164164
char errmsg[MAX_STR];
165165
sprintf(errmsg, MSG(WORK_AREA), g->Message);
166166
strcpy(g->Message, errmsg);
167-
g->Sarea_Size = 0;
168-
} else
169-
g->Sarea_Size = worksize;
167+
} // endif Sarea
170168

171169
g->jump_level = -1; /* New setting to allow recursive call of Plug */
172170
return(g);
@@ -183,15 +181,7 @@ int PlugExit(PGLOBAL g)
183181
if (dup)
184182
free(dup);
185183

186-
if (g->Sarea) {
187-
#if !defined(DEVELOPMENT)
188-
if (trace)
189-
#endif
190-
htrc("Freeing Sarea at %p size=%d\n", g->Sarea, g->Sarea_Size);
191-
192-
free(g->Sarea);
193-
} // endif Sarea
194-
184+
FreeSarea(g);
195185
delete g;
196186
} // endif g
197187

@@ -459,30 +449,65 @@ short GetLineLength(PGLOBAL g)
459449
/***********************************************************************/
460450
/* Program for memory allocation of work and language areas. */
461451
/***********************************************************************/
462-
void *PlugAllocMem(PGLOBAL g, uint size)
452+
bool AllocSarea(PGLOBAL g, uint size)
463453
{
464-
void *areap; /* Pointer to allocated area */
465-
466454
/*********************************************************************/
467455
/* This is the allocation routine for the WIN32/UNIX/AIX version. */
468456
/*********************************************************************/
469-
if (!(areap = malloc(size)))
470-
sprintf(g->Message, MSG(MALLOC_ERROR), "malloc");
457+
#if defined(__WIN__)
458+
if (size >= 1048576) // 1M
459+
g->Sarea = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
460+
else
461+
#endif
462+
g->Sarea = malloc(size);
463+
464+
if (!g->Sarea) {
465+
sprintf(g->Message, MSG(MALLOC_ERROR), "malloc");
466+
g->Sarea_Size = 0;
467+
} else
468+
g->Sarea_Size = size;
471469

472470
#if defined(DEVELOPMENT)
473471
if (true) {
474472
#else
475473
if (trace) {
476474
#endif
477-
if (areap)
478-
htrc("Memory of %u allocated at %p\n", size, areap);
475+
if (g->Sarea)
476+
htrc("Work area of %u allocated at %p\n", size, g->Sarea);
479477
else
480-
htrc("PlugAllocMem: %s\n", g->Message);
478+
htrc("SareaAlloc: %s\n", g->Message);
481479

482480
} // endif trace
483481

484-
return (areap);
485-
} // end of PlugAllocMem
482+
return (!g->Sarea);
483+
} // end of AllocSarea
484+
485+
/***********************************************************************/
486+
/* Program for memory freeing the work area. */
487+
/***********************************************************************/
488+
void FreeSarea(PGLOBAL g)
489+
{
490+
if (g->Sarea) {
491+
#if defined(__WIN__)
492+
if (g->Sarea_Size >= 1048576) // 1M
493+
VirtualFree(g->Sarea, 0, MEM_RELEASE);
494+
else
495+
#endif
496+
free(g->Sarea);
497+
498+
#if defined(DEVELOPMENT)
499+
if (true)
500+
#else
501+
if (trace)
502+
#endif
503+
htrc("Freeing Sarea at %p size = %d\n", g->Sarea, g->Sarea_Size);
504+
505+
g->Sarea = NULL;
506+
g->Sarea_Size = 0;
507+
} // endif Sarea
508+
509+
return;
510+
} // end of FreeSarea
486511

487512
/***********************************************************************/
488513
/* Program for SubSet initialization of memory pools. */

storage/connect/user_connect.cc

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,20 @@ void user_connect::SetHandler(ha_connect *hc)
156156
bool user_connect::CheckCleanup(bool force)
157157
{
158158
if (thdp->query_id > last_query_id || force) {
159-
uint worksize= GetWorkSize();
159+
uint worksize= GetWorkSize(), size = g->Sarea_Size;
160160

161161
PlugCleanup(g, true);
162162

163-
if (g->Sarea_Size != worksize) {
164-
if (g->Sarea) {
165-
#if !defined(DEVELOPMENT)
166-
if (trace)
167-
#endif
168-
htrc("CheckCleanup: Free Sarea at %p size=%d\n",
169-
g->Sarea, g->Sarea_Size);
170-
171-
free(g->Sarea);
172-
} // endif Size
163+
if (size != worksize) {
164+
FreeSarea(g);
173165

174166
// Check whether the work area could be allocated
175-
if (!(g->Sarea = PlugAllocMem(g, worksize))) {
176-
g->Sarea = PlugAllocMem(g, g->Sarea_Size);
167+
if (AllocSarea(g, worksize)) {
168+
AllocSarea(g, size);
177169
SetWorkSize(g->Sarea_Size); // Was too big
178-
} else
179-
g->Sarea_Size = worksize; // Ok
170+
} // endif sarea
180171

181-
} // endif worksize
172+
} // endif worksize
182173

183174
PlugSubSet(g, g->Sarea, g->Sarea_Size);
184175
g->Xchk = NULL;

0 commit comments

Comments
 (0)