Skip to content

Commit 5b974f0

Browse files
Fixing some memory issues
1 parent 9fb595b commit 5b974f0

3 files changed

Lines changed: 34 additions & 37 deletions

File tree

modules/topology_hiding/th_no_dlg_logic.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
#define START_THINFO_BUF_SZ 1000
3636
#define THINFO_MAX_BUFFER_SIZE 10000
37-
#define MAX_ENCODED_SIP_URIS 12
3837

3938
#define TOPOH_MATCH_TAG_MATCH 2
4039
#define TOPOH_MATCH_SUCCESS 1
@@ -184,7 +183,7 @@ int topo_hiding_no_dlg(struct sip_msg *req, struct cell* t, unsigned int extra_f
184183
LM_WARN("Cannot store DID in user when dialog support is not engaged!\n");
185184

186185
if (!(extra_flags & TOPOH_KEEP_USER) && params && params->ct_callee_user.len) {
187-
param_size = sizeof *p + params->ct_callee_user.len + sizeof(uint16_t);
186+
param_size = sizeof *p + params->ct_callee_user.len;
188187
} else {
189188
param_size = sizeof *p;
190189
}
@@ -199,15 +198,11 @@ int topo_hiding_no_dlg(struct sip_msg *req, struct cell* t, unsigned int extra_f
199198

200199
if (!(extra_flags & TOPOH_KEEP_USER)) {
201200
if (params && params->ct_callee_user.len) {
202-
p = shm_malloc(sizeof *p + params->ct_callee_user.len);
203-
if (p) {
204-
memset(p, 0, sizeof *p);
205-
p->username.s = (char *)(p + 1);
206-
p->username.len = params->ct_callee_user.len;
207-
memcpy(p->username.s, params->ct_callee_user.s,
208-
params->ct_callee_user.len);
209-
username = &params->ct_callee_user;
210-
}
201+
p->username.s = (char *)(p + 1);
202+
p->username.len = params->ct_callee_user.len;
203+
memcpy(p->username.s, params->ct_callee_user.s,
204+
params->ct_callee_user.len);
205+
username = &params->ct_callee_user;
211206
}
212207
}
213208

@@ -477,7 +472,6 @@ static struct lump *anchor_after_last_record_route(struct sip_msg *msg) {
477472
return anchor_lump(msg, offset, HDR_RECORDROUTE_T);
478473
}
479474

480-
// TODO log callId perhaps?
481475
static void th_no_dlg_onreply(struct cell *t, int type, struct tmcb_params *param) {
482476
struct th_no_dlg_param *p = *(param->param);
483477
str route_s = STR_NULL;
@@ -680,6 +674,9 @@ static inline int th_no_dlg_onrequest(struct sip_msg *req, uint16_t flags, str *
680674
return -1;
681675
}
682676

677+
if (route_s.s != NULL)
678+
pkg_free(route_s.s);
679+
683680
return 1;
684681
error:
685682
if (route_s.s != NULL) {
@@ -793,8 +790,10 @@ static char* build_encoded_contact_suffix_legacy(struct sip_msg* msg, str rr_set
793790
}
794791
}
795792

796-
if (head != NULL)
797-
pkg_free(head);
793+
if (head != NULL) {
794+
free_rr(&head);
795+
head = NULL;
796+
}
798797

799798
total_len += params_len;
800799

@@ -881,6 +880,8 @@ static char* build_encoded_contact_suffix_legacy(struct sip_msg* msg, str rr_set
881880
pkg_free(suffix_plain);
882881
if (rr_set_free_str)
883882
pkg_free(rr_set_free_str);
883+
if (head)
884+
free_rr(&head);
884885
return NULL;
885886
}
886887

@@ -1031,7 +1032,7 @@ static char* build_encoded_thinfo_suffix(struct sip_msg* msg, str rr_set, int *s
10311032
}
10321033

10331034
if (head != NULL) {
1034-
pkg_free(head);
1035+
free_rr(&head);
10351036
head = NULL;
10361037
}
10371038

@@ -1097,15 +1098,12 @@ static char* build_encoded_thinfo_suffix(struct sip_msg* msg, str rr_set, int *s
10971098

10981099
return suffix_enc;
10991100
error:
1100-
if (rr_set_free_str) {
1101+
if (rr_set_free_str)
11011102
pkg_free(rr_set_free_str);
1102-
}
1103-
if (head) {
1104-
pkg_free(head);
1105-
}
1106-
if (suffix_enc) {
1103+
if (head)
1104+
free_rr(&head);
1105+
if (suffix_enc)
11071106
pkg_free(suffix_enc);
1108-
}
11091107
return NULL;
11101108
}
11111109

@@ -1460,17 +1458,17 @@ static decoded_info_buffer_t decode_info_buffer_legacy(str *info, const thinfo_o
14601458
for (i = 0; i < dec_len; i++)
14611459
dec_buf_legacy[i] ^= options->param_password.s[i % options->param_password.len];
14621460

1463-
#define __extract_len_and_buf(_p, _len, _s) \
1464-
do { \
1465-
(_s).len = *(short *)p;\
1466-
if ((_s).len < 0 || (_s).len > _len) {\
1467-
LM_ERR("bad length %d in encoded contact\n", (_s).len); \
1468-
goto error;\
1469-
}\
1470-
(_s).s = _p + sizeof(short);\
1471-
_p += sizeof(short) + (_s).len;\
1472-
_len -= sizeof(short) + (_s).len;\
1473-
} while(0)
1461+
#define __extract_len_and_buf(_p, _len, _s) \
1462+
do { \
1463+
memcpy(&(_s).len, _p, sizeof(short)); \
1464+
if ((_s).len < 0 || (_s).len > _len) { \
1465+
LM_ERR("bad length %d in encoded contact\n", (_s).len); \
1466+
goto error; \
1467+
} \
1468+
(_s).s = _p + sizeof(short); \
1469+
_p += sizeof(short) + (_s).len; \
1470+
_len -= sizeof(short) + (_s).len; \
1471+
} while(0)
14741472

14751473
p = dec_buf_legacy;
14761474
size = dec_len;

modules/topology_hiding/th_no_dlg_logic.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
enum encode_scheme {ENC_BASE64, ENC_BASE32};
3232

3333
#define TH_INFO_PASSWORD_ROTATION_SIZE 2
34+
#define MAX_ENCODED_SIP_URIS 12
3435

3536
#define DEFAULT_PARAM str_init("thinfo")
3637
#define DEFAULT_PW str_init("ToPoCtPaSS")
@@ -41,7 +42,7 @@ typedef struct {
4142
int compact_encoding;
4243
} thinfo_options_t;
4344

44-
extern str decoded_uris[12];
45+
extern str decoded_uris[MAX_ENCODED_SIP_URIS];
4546
extern int decoded_uris_count;
4647
extern int ctx_decoded_routes_valid_idx;
4748

modules/topology_hiding/topology_hiding.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int auto_route_on_trusted_socket = 1;
5555
int th_ct_enc_scheme;
5656

5757
/* Global buffer for decoded routes */
58-
str decoded_uris[12];
58+
str decoded_uris[MAX_ENCODED_SIP_URIS];
5959
int decoded_uris_count = 0;
6060

6161
/* Context flag to track if decoded routes are valid for current message */
@@ -384,8 +384,6 @@ int w_topology_hiding_match(struct sip_msg *req, void *seq_match_mode_val)
384384
}
385385

386386
static char *callid_buf=NULL;
387-
static int callid_buf_len=0;
388-
389387
static int pv_topo_callee_callid(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
390388
{
391389
struct dlg_cell *dlg;

0 commit comments

Comments
 (0)