2

I am trying to run MySQL UDF written in C++. It compiles fine and generates the correct output, but it generates a lot of junk after the output. I would like to know the reason for this junk and how I can resolve this problem? I have attached my code and a screenshot of the output.

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "mysql-connector-c-6.1.9-linux-glibc2.5-x86_64/include/mysql.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/mysql_connection.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/sqlstring.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/datatype.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/resultset_metadata.h"
#include "mysql-connector-c++-1.1.8-linux-ubuntu16.04-x86-64bit/include/cppconn/exception.h"

using namespace std;

extern "C" {
    char *hello_world (UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long length,char *is_null, char *error);
    my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message);
    void hello_world_deinit (UDF_INIT *initid);
    //template <typename T>  T adder (UDF_INIT *initid, UDF_eARGS *args,string result, unsigned long length,string is_null, string error,T v);
}

char *hello_world (UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long length, char *is_null, char *error)
{
    string res;
    res = args->args[0];
    res.append(" hello");
    char *c = new char[res.size()];
    strcpy(c, res.c_str());
    return c;
}

my_bool hello_world_init (UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    return 0;
    //cout<<"success"<<endl;
}

void hello_world_deinit (UDF_INIT *initid)
{
    return;
}

enter image description here

2
  • The array you allocate as result is too small. You need to add one char for the terminating zero: new char[res.size() + 1]. Commented Apr 11, 2017 at 9:51
  • The buffer you allocate for the result will cause a memory leak. You need to manage the memory in the xxx_init and xxx_deinit functions: pre-allocate a buffer to store the result and free it afterwards. Commented Apr 11, 2017 at 10:06

1 Answer 1

4

The signature of the hello_world function is wrong. The fourth parameter should be

unsigned long *length

The value pointed to by that pointer has to be set to the length of the returned string.

char *c = new char[res.size() + 1];
strcpy(c, res.c_str());
*length = res.size();
return c;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.