Skip to content

Commit

Permalink
Fix mismatched wrapped/unwrapped memory alloc/free in properties.
Browse files Browse the repository at this point in the history
Closes #3192. Thanks to Russell King.
  • Loading branch information
ralight committed Jan 20, 2025
1 parent ba2b98d commit 015fe3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Broker:
Closes #2325.
- Fix `bind_interface` producing an error when used with an interface that has
an IPv6 link-local address and no other IPv6 addresses. Closes #2696.
- Fix mismatched wrapped/unwrapped memory alloc/free in properties. Closes #3192.

Client library:
- Fix threads linking on Windows for static libmosquitto library
Expand Down
22 changes: 11 additions & 11 deletions lib/property_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void property__free(mosquitto_property **property)
break;
}

free(*property);
mosquitto__free(*property);
*property = NULL;
}

Expand Down Expand Up @@ -1120,7 +1120,7 @@ const mosquitto_property *mosquitto_property_read_binary(const mosquitto_propert

if(value){
*len = p->value.bin.len;
*value = calloc(1, *len + 1U);
*value = mosquitto__calloc(1, *len + 1U);
if(!(*value)) return NULL;

memcpy(*value, p->value.bin.v, *len);
Expand Down Expand Up @@ -1149,7 +1149,7 @@ const mosquitto_property *mosquitto_property_read_string(const mosquitto_propert
}

if(value){
*value = calloc(1, (size_t)p->value.s.len+1);
*value = mosquitto__calloc(1, (size_t)p->value.s.len+1);
if(!(*value)) return NULL;

memcpy(*value, p->value.s.v, p->value.s.len);
Expand All @@ -1172,16 +1172,16 @@ const mosquitto_property *mosquitto_property_read_string_pair(const mosquitto_pr
if(p->identifier != MQTT_PROP_USER_PROPERTY) return NULL;

if(name){
*name = calloc(1, (size_t)p->name.len+1);
*name = mosquitto__calloc(1, (size_t)p->name.len+1);
if(!(*name)) return NULL;
memcpy(*name, p->name.v, p->name.len);
}

if(value){
*value = calloc(1, (size_t)p->value.s.len+1);
*value = mosquitto__calloc(1, (size_t)p->value.s.len+1);
if(!(*value)){
if(name){
free(*name);
mosquitto__free(*name);
*name = NULL;
}
return NULL;
Expand All @@ -1203,7 +1203,7 @@ int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_prope
*dest = NULL;

while(src){
pnew = calloc(1, sizeof(mosquitto_property));
pnew = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!pnew){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand Down Expand Up @@ -1255,7 +1255,7 @@ int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_prope
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
pnew->value.s.len = src->value.s.len;
pnew->value.s.v = src->value.s.v ? strdup(src->value.s.v) : (char*)calloc(1,1);
pnew->value.s.v = src->value.s.v ? mosquitto__strdup(src->value.s.v) : (char*)mosquitto__calloc(1,1);
if(!pnew->value.s.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand All @@ -1265,7 +1265,7 @@ int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_prope
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
pnew->value.bin.len = src->value.bin.len;
pnew->value.bin.v = malloc(pnew->value.bin.len);
pnew->value.bin.v = mosquitto__malloc(pnew->value.bin.len);
if(!pnew->value.bin.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand All @@ -1275,14 +1275,14 @@ int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_prope

case MQTT_PROP_USER_PROPERTY:
pnew->value.s.len = src->value.s.len;
pnew->value.s.v = src->value.s.v ? strdup(src->value.s.v) : (char*)calloc(1,1);
pnew->value.s.v = src->value.s.v ? mosquitto__strdup(src->value.s.v) : (char*)mosquitto__calloc(1,1);
if(!pnew->value.s.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}

pnew->name.len = src->name.len;
pnew->name.v = src->name.v ? strdup(src->name.v) : (char*)calloc(1,1);
pnew->name.v = src->name.v ? mosquitto__strdup(src->name.v) : (char*)mosquitto__calloc(1,1);
if(!pnew->name.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand Down

0 comments on commit 015fe3d

Please sign in to comment.