commit 4ee4c4f7bb7a4a9874c6ab8cca639c3e8d9b2cb3
parent a1db8461c3fa3e1d946070c38f6c099e4e1c2e87
Author: Claudio Alessi <smoppy@gmail.com>
Date: Thu, 3 Mar 2016 17:34:43 +0100
Use mysql_fetch_lengths() to allocate items.
This is not an optimization but a bugfix. Here is an excerpt from the MySQL
documentation: "you must use this function to determine the size of the data,
because strlen() returns incorrect results for any field containing null
characters."
Diffstat:
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/core.c b/core.c
@@ -268,6 +268,7 @@ mysql_items(MYSQL_RES *res, Item **items) {
MYSQL_ROW row;
Item *item;
int i, nfds, nrows;
+ unsigned long *lens;
nfds = mysql_num_fields(res);
nrows = mysql_num_rows(res);
@@ -277,11 +278,11 @@ mysql_items(MYSQL_RES *res, Item **items) {
item = ecalloc(1, sizeof(Item));
item->nfields = nfds;
if(nfds) {
+ lens = mysql_fetch_lengths(res);
item->fields = ecalloc(nfds, sizeof(char *));
for(i = 0; i < nfds; ++i) {
- /* MySQL max column name length is 64 */
- item->fields[i] = ecalloc(64, sizeof(char));
- snprintf(item->fields[i], 64, "%s", row[i]);
+ item->fields[i] = ecalloc(lens[i], sizeof(char));
+ snprintf(item->fields[i], lens[i], "%s", row[i]);
}
}
attachitemto(item, &(*items));