Skip to content
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

Wrong memory allocation with init? #9

Open
juanmirocks opened this issue Jun 2, 2015 · 4 comments
Open

Wrong memory allocation with init? #9

juanmirocks opened this issue Jun 2, 2015 · 4 comments

Comments

@juanmirocks
Copy link
Owner

From Pablo Ruano (in Spanish):

Creo que tiene el siguiente problema:

- En la función levenshtein_init pides la memoria necesaria para calcular la distancia según la longitud de las dos cadenas que se pasan como argumento:

malloc(sizeof(int) * (args->lengths[0] + 1) * (args->lengths[1] + 1))

- MySQL sólo ejecuta la función init la primera vez que va a usar la función, las siguientes veces llama directamente a la función levenshtein. Puedes leerlo aquí:

https://dev.mysql.com/doc/refman/5.6/en/adding-udf.html

- Por tanto, en sucesivas llamadas, si las cadenas de entrada son de longitud superior a las que se usaron por primera vez con la función init, la función levenshtein usará memora sin haberla reservado previamente, haciendo que la estabilidad de MySQL se vea comprometida.

He visto otras funciones UDF, y lo que suelen hacer en estos casos en los que se necesita usar memoria de forma dinámica es reservarla directamente en la función, no en el init.

Saludos
@nachitox
Copy link

Is this really a problem?

@nachitox
Copy link

nachitox commented Feb 19, 2019

I went to the docs page linked in the original post and found this:

https://dev.mysql.com/doc/refman/5.6/en/udf-arguments.html

unsigned long *lengths

For the initialization function, the lengths array indicates the maximum string length for each argument. You should not change these. For each invocation of the main function, lengths contains the actual lengths of any string arguments that are passed for the row currently being processed. For arguments of types INT_RESULT or REAL_RESULT, lengths still contains the maximum length of the argument (as for the initialization function).

So, calling malloc(sizeof(int) * (args->lengths[0] + 1) * (args->lengths[1] + 1)) on the init function is the right thing to do, which would allocate memory for the maximum length allowed

@juanmirocks
Copy link
Owner Author

juanmirocks commented Feb 20, 2019 via email

@laneme
Copy link

laneme commented Oct 24, 2019

Just pinging a little bit. Although I have no idea about this issue(?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants