Skip to content

Update lib_mysqludf_sys.c #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions lib_mysqludf_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ my_bool sys_eval_init(
void sys_eval_deinit(
UDF_INIT *initid
){
if(initid->ptr)
{
free(initid->ptr);
initid->ptr = NULL;
}
}
char* sys_eval(
UDF_INIT *initid
Expand All @@ -394,32 +399,46 @@ char* sys_eval(
, char *is_null
, char *error
){
FILE *pipe;
char line[1024];
unsigned long outlen, linelen;

result = malloc(1);
outlen = 0;
// Rewrite by ZhengWang [email protected]
// result is char[256], we could use it or we alloc new buffer & free it at deinit
FILE *pipe = popen(args->args[0], "r");
if(!pipe)
{
*error = 1;
*length = 0;
*is_null = 1;
initid->ptr = NULL;
memset(result, 0, 256);

pipe = popen(args->args[0], "r");
return result;
}

while (fgets(line, sizeof(line), pipe) != NULL) {
linelen = strlen(line);
result = realloc(result, outlen + linelen);
strncpy(result + outlen, line, linelen);
outlen = outlen + linelen;
unsigned len_ = fread(result, 1, 256, pipe);
if( len_ < 256 )
{
// For Length < 256, we store string in result, and return it directly
pclose(pipe);
*length = len_;
return result;
}

pclose(pipe);
char* buf_ = (char*)malloc(len_);
unsigned long bufLen_ = len_;
memcpy(buf_ + 0, result, len_);

if (!(*result) || result == NULL) {
*is_null = 1;
} else {
result[outlen] = 0x00;
*length = strlen(result);
while((len_ = fread(result, 1, 256, pipe)) > 0 )
{
buf_ = (char*)realloc(buf_, bufLen_ + len_);
memcpy(buf_ + bufLen_, result, len_);
bufLen_ += len_;
}

pclose(pipe);
*length = bufLen_;
initid->ptr = buf_;
memset(result, 0, 256);

return result;
return buf_;
}


Expand Down