--- libaitmqtt/src/aitmqtt.c 2012/04/27 16:02:16 1.1.1.1.2.10 +++ libaitmqtt/src/aitmqtt.c 2012/04/27 16:40:23 1.1.1.1.2.13 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitmqtt.c,v 1.1.1.1.2.10 2012/04/27 16:02:16 misho Exp $ +* $Id: aitmqtt.c,v 1.1.1.1.2.13 2012/04/27 16:40:23 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -400,19 +400,27 @@ mqtt_subCopy(mqtt_subscr_t * __restrict dst, mqtt_subs return NULL; if (src->sub_topic.msg_base) { - dst->sub_topic.msg_base = malloc(src->sub_topic.msg_len); + dst->sub_topic.msg_base = malloc(src->sub_topic.msg_len + 1); if (!dst->sub_topic.msg_base) { LOGERR; memset(dst, 0, sizeof(mqtt_subscr_t)); return NULL; } else { dst->sub_topic.msg_len = src->sub_topic.msg_len; + ((char*) dst->sub_topic.msg_base)[dst->sub_topic.msg_len] = 0; memcpy(dst->sub_topic.msg_base, src->sub_topic.msg_base, dst->sub_topic.msg_len); } + } else { + /* + if (dst->sub_topic.msg_base) + free(dst->sub_topic.msg_base); + */ + dst->sub_topic.msg_base = NULL; + dst->sub_topic.msg_len = 0; } if (src->sub_value.msg_base) { - dst->sub_value.msg_base = malloc(src->sub_value.msg_len); + dst->sub_value.msg_base = malloc(src->sub_value.msg_len + 1); if (!dst->sub_value.msg_base) { LOGERR; if (dst->sub_topic.msg_base) @@ -421,9 +429,17 @@ mqtt_subCopy(mqtt_subscr_t * __restrict dst, mqtt_subs return NULL; } else { dst->sub_value.msg_len = src->sub_value.msg_len; + ((char*) dst->sub_value.msg_base)[dst->sub_value.msg_len] = 0; memcpy(dst->sub_value.msg_base, src->sub_value.msg_base, dst->sub_value.msg_len); } + } else { + /* + if (dst->sub_value.msg_base) + free(dst->sub_value.msg_base); + */ + dst->sub_value.msg_base = NULL; + dst->sub_value.msg_len = 0; } dst->sub_ret = src->sub_ret;