From 4c21afdbbce662e17c335876d3749b52fe907e96 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Fri, 29 May 2015 17:52:17 +0200 Subject: [PATCH] mx_mysql: Fix serious bug when fetching empty strings (length=0) MySQL will not mark empty strings truncated because the length of an empty string is equal to 0 and it occupies 0 bytes in MySQL. So the error flag will never be set for zero length strings. Since we need zero length strings to be \0 terminated we ignore error conditions for string types. --- mx_mysql.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mx_mysql.c b/mx_mysql.c index 33557f29..7be27a79 100644 --- a/mx_mysql.c +++ b/mx_mysql.c @@ -994,9 +994,6 @@ int mx_mysql_statement_fetch(struct mx_mysql_stmt *stmt) r = &stmt->result; for (col = 0; col < r->count; col++) { - if (!(r->data[col].is_error)) - continue; - if (r->bind[col].buffer_type == MYSQL_TYPE_STRING) { str = mx_calloc_forever(r->data[col].length + 1, sizeof(*str)); @@ -1012,6 +1009,9 @@ int mx_mysql_statement_fetch(struct mx_mysql_stmt *stmt) continue; } + if (!(r->data[col].is_error)) + continue; + mx_log_debug("WARNING: result data returned in column with index %d was truncated. query was:", col); mx_log_debug(" \\ %s", stmt->statement); no_error = 0;