Skip to content

Commit 54097db

Browse files
committed
messaging: add magic
to eg. check double-free (in deleter set to 0) or anyhow wrong pointer
1 parent cd32693 commit 54097db

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/messaging.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* UltraGrid modules.
77
*/
88
/*
9-
* Copyright (c) 2013-2025 CESNET
9+
* Copyright (c) 2013-2026 CESNET, zájmové sdružení právnických osob
1010
* All rights reserved.
1111
*
1212
* Redistribution and use in source and binary forms, with or without
@@ -55,16 +55,19 @@
5555
#include "module.h"
5656
#include "utils/list.h"
5757
#include "utils/lock_guard.h"
58-
#include "utils/macros.h" // for snprintf_ch
58+
#include "utils/macros.h" // for snprintf_ch, to_fourcc
5959

6060
#define MAX_MESSAGES 100
6161
#define MAX_MESSAGES_FOR_NOT_EXISTING_RECV 10
6262
#define MOD_NAME "[messaging] "
63+
#define MSG_MAGIC to_fourcc('m', 'e', 's', 'g')
64+
#define RESP_MAGIC to_fourcc('r', 'e', 's', 'p')
6365

6466
using namespace std;
6567
using namespace ultragrid;
6668

6769
struct response {
70+
uint32_t magic;
6871
int status;
6972
char text[];
7073
};
@@ -323,6 +326,7 @@ struct message *new_message(size_t len)
323326

324327
struct message *ret = (struct message *)
325328
calloc(1, len);
329+
ret->magic = MSG_MAGIC;
326330

327331
return ret;
328332
}
@@ -338,6 +342,8 @@ void free_message(struct message *msg, struct response *r)
338342
return;
339343
}
340344

345+
assert(msg->magic == MSG_MAGIC);
346+
341347
if (r) {
342348
if (msg->send_response) {
343349
msg->send_response(msg->priv_data, r);
@@ -354,6 +360,7 @@ void free_message(struct message *msg, struct response *r)
354360
delete (shared_ptr<struct responder> *) msg->priv_data;
355361
}
356362

363+
msg->magic = 0;
357364
free(msg);
358365
}
359366

@@ -366,6 +373,7 @@ void free_message(struct message *msg, struct response *r)
366373
struct response *new_response(int status, const char *text)
367374
{
368375
struct response *resp = (struct response *) malloc(sizeof(struct response) + (text ? strlen(text) : 0) + 1);
376+
resp->magic = RESP_MAGIC;
369377
resp->status = status;
370378
if (text) {
371379
strcpy(resp->text, text);
@@ -376,6 +384,11 @@ struct response *new_response(int status, const char *text)
376384
}
377385

378386
void free_response(struct response *r) {
387+
if (r == nullptr) {
388+
return;
389+
}
390+
assert(r->magic == RESP_MAGIC);
391+
r->magic = 0;
379392
free(r);
380393
}
381394

src/messaging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ struct response;
7070
struct message;
7171

7272
struct message {
73+
alignas(8) uint32_t magic;
7374
/**
7475
* Individual message types may have defined custom data deleters.
7576
* Please note that the deleter must not delete the struct itself.
7677
*/
77-
alignas(8) void (*data_deleter)(struct message *);
78+
void (*data_deleter)(struct message *);
7879

7980
// following members are used internally and should not be touched anyhow
8081
// except from messaging.cpp

0 commit comments

Comments
 (0)