--- libaitmqtt/src/aitmqtt.c 2012/04/25 16:25:52 1.1.1.1.2.6 +++ libaitmqtt/src/aitmqtt.c 2012/04/27 08:12:30 1.1.1.1.2.9 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitmqtt.c,v 1.1.1.1.2.6 2012/04/25 16:25:52 misho Exp $ +* $Id: aitmqtt.c,v 1.1.1.1.2.9 2012/04/27 08:12:30 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -270,16 +270,16 @@ mqtt_sizeLen(u_int len) } /* - * mqtt_str2sub Create MQTT subscribe variable from string(s) + * mqtt_str2subs Create MQTT subscribe variable from string(s) * - * @csStr = strings - * @strnum = number of strings elements + * @csStr = null terminated string array + * @strnum = copy at most number of strings elements * @qoses = QoS elements applied to subscribe variable, * count of elements must be equal with csStr elements * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub() */ inline mqtt_subscr_t * -mqtt_str2sub(const char **csStr, u_short strnum, u_char *qoses) +mqtt_str2subs(const char **csStr, u_short strnum, u_char *qoses) { mqtt_subscr_t *v; register int i, items; @@ -287,12 +287,11 @@ mqtt_str2sub(const char **csStr, u_short strnum, u_cha if (!csStr) return NULL; - for (items = 0, strs = csStr; *strs; items++, strs++) - if (strnum && items >= strnum) { - items = strnum; - break; - } + for (items = 0, strs = csStr; + (!strnum || (strnum && items < strnum)) && *strs; + items++, strs++); + if (!(v = malloc((items + 1) * sizeof(mqtt_subscr_t)))) { LOGERR; return NULL; @@ -368,17 +367,23 @@ mqtt_subAlloc(u_short num) * return: NULL error or subscribe array, after use must call mqtt_subFree() */ inline mqtt_subscr_t * -mqtt_subRealloc(mqtt_subscr_t * __restrict subs, u_short num) +mqtt_subRealloc(mqtt_subscr_t ** __restrict subs, u_short num) { mqtt_subscr_t *s = NULL; - s = realloc(subs, (num + 1) * sizeof(mqtt_subscr_t)); + if (!subs) + return NULL; + + s = realloc(*subs, (num + 1) * sizeof(mqtt_subscr_t)); if (!s) { LOGERR; return NULL; + } else { + memset(s + num, 0, sizeof(mqtt_subscr_t)); + *subs = s; } - return s; + return *subs; } /*