Levenshtein-MySQL-UDF icon indicating copy to clipboard operation
Levenshtein-MySQL-UDF copied to clipboard

Wrong memory allocation with init?

Open juanmirocks opened this issue 10 years ago • 4 comments

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

juanmirocks avatar Jun 02 '15 08:06 juanmirocks

Is this really a problem?

nachitox avatar Feb 14 '19 13:02 nachitox

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

nachitox avatar Feb 19 '19 13:02 nachitox

Thank you. Unable to look at this issue right now. Will come back at it later. Thank you for posting! On Tue 19. Feb 2019 at 14:32, Ignacio colautti [email protected] wrote:

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

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/juanmirocks/Levenshtein-MySQL-UDF/issues/9#issuecomment-465128999, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGQH4OM7EThhpEybsxignYkBC_Tln_qks5vO_z2gaJpZM4E0R4n .

-- Juanmi, CEO and co-founder @ 🍃tagtog.net

Follow tagtog updates on 🐦 Twitter: @tagtog_net

https://twitter.com/tagtog_net

juanmirocks avatar Feb 20 '19 21:02 juanmirocks

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

laneme avatar Oct 24 '19 09:10 laneme