Skip to content

Commit e3bc133

Browse files
Luu Quang Minhminminlittleshrimp
authored andcommitted
[FIX][REFACTOR] DLTv2 protocol byte-order and memory leaks
Fix big-endian encoding for v2 multi-byte header fields: - Add DLT_HTOBE_16 for baseheaderv2->len on all write paths - Add DLT_BETOH_16 for baseheaderv2->len on receive path - Add DLT_HTOBE_32 for nanoseconds, msid, seid, linr fields - Fix monotonic timestamp flag from 0x8000 to 0x80000000 (bit 31) - Uncomment byte-swap in dlt_user_print_msg_v2 Fix memory leaks detected by AddressSanitizer: - Free tag array in dlt_message_free_v2 and before re-allocation - Add dlt_message_free_v2 for msgv2 in dlt_daemon_local_cleanup - Remove unnecessary malloc for DltDaemonApplication pointer - Free buffer, apid, ctid on all exit paths in register_context - Free apid2/ctid2 in dlt_daemon_contexts_clear Signed-off-by: LUU QUANG MINH <Minh.LuuQuang@vn.bosch.com>
1 parent d9a2251 commit e3bc133

6 files changed

Lines changed: 81 additions & 30 deletions

File tree

src/daemon/dlt-daemon.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,7 @@ void dlt_daemon_local_cleanup(DltDaemon *daemon, DltDaemonLocal *daemon_local, i
24422442
dlt_event_handler_cleanup_connections(&daemon_local->pEvent);
24432443

24442444
dlt_message_free(&(daemon_local->msg), daemon_local->flags.vflag);
2445+
dlt_message_free_v2(&(daemon_local->msgv2), daemon_local->flags.vflag);
24452446

24462447
/* free shared memory */
24472448
if (daemon_local->flags.offlineTraceDirectory[0])
@@ -2709,7 +2710,7 @@ int dlt_daemon_log_internal(DltDaemon *daemon, DltDaemonLocal *daemon_local,
27092710
msg.headerextrav2.seconds[2]=(uint8_t)((t >> 16) & 0xFF);
27102711
msg.headerextrav2.seconds[3]=(uint8_t)((t >> 8) & 0xFF);
27112712
msg.headerextrav2.seconds[4]= (uint8_t)(t & 0xFF);
2712-
msg.headerextrav2.nanoseconds |= 0x8000;
2713+
msg.headerextrav2.nanoseconds |= 0x80000000;
27132714
}
27142715
#else
27152716
struct timespec ts;
@@ -2731,7 +2732,7 @@ int dlt_daemon_log_internal(DltDaemon *daemon, DltDaemonLocal *daemon_local,
27312732
if (ts.tv_nsec < 0x3B9ACA00) {
27322733
msg.headerextrav2.nanoseconds = (uint32_t) ts.tv_nsec; /* value is long */
27332734
}
2734-
msg.headerextrav2.nanoseconds |= 0x8000;
2735+
msg.headerextrav2.nanoseconds |= 0x80000000;
27352736
}
27362737
#endif
27372738

@@ -2801,7 +2802,7 @@ int dlt_daemon_log_internal(DltDaemon *daemon, DltDaemonLocal *daemon_local,
28012802
msg.datasize += uiSize;
28022803

28032804
/* Calc length */
2804-
msg.baseheaderv2->len = (uint16_t)(msg.headersizev2 - (int32_t)msg.storageheadersizev2 + msg.datasize);
2805+
msg.baseheaderv2->len = DLT_HTOBE_16((uint16_t)(msg.headersizev2 - (int32_t)msg.storageheadersizev2 + msg.datasize));
28052806

28062807
dlt_daemon_client_send_v2(DLT_DAEMON_SEND_TO_ALL, daemon,daemon_local,
28072808
msg.headerbufferv2, (int)msg.storageheadersizev2,
@@ -4239,6 +4240,7 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
42394240
if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
42404241
dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
42414242
__func__);
4243+
free(buffer);
42424244
return -1;
42434245
}
42444246

@@ -4252,10 +4254,11 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
42524254
(unsigned int)len,
42534255
DLT_RCV_SKIP_HEADER);
42544256

4255-
if (temp < 0)
4257+
if (temp < 0) {
42564258
/* Not enough bytes received */
4259+
free(buffer);
42574260
return -1;
4258-
else {
4261+
} else {
42594262
to_remove = (uint32_t) temp;
42604263
}
42614264

@@ -4265,6 +4268,7 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
42654268
usercontext.apid = (char *)malloc((size_t)usercontext.apidlen + 1);
42664269
if (usercontext.apid == NULL) {
42674270
dlt_log(LOG_ERR, "Memory allocation failed for usercontext.apid\n");
4271+
free(buffer);
42684272
return -1;
42694273
}
42704274
memcpy(usercontext.apid, (buffer + offset), usercontext.apidlen);
@@ -4275,6 +4279,8 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
42754279
usercontext.ctid = (char *)malloc((size_t)usercontext.ctidlen + 1);
42764280
if (usercontext.ctid == NULL) {
42774281
dlt_log(LOG_ERR, "Memory allocation failed for usercontext.ctid\n");
4282+
free(usercontext.apid);
4283+
free(buffer);
42784284
return -1;
42794285
}
42804286
memcpy(usercontext.ctid, (buffer + offset), usercontext.ctidlen);
@@ -4319,6 +4325,9 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
43194325
/* We can now remove data. */
43204326
if (dlt_receiver_remove(rec, (int) to_remove) != DLT_RETURN_OK) {
43214327
dlt_log(LOG_WARNING, "Can't remove bytes from receiver\n");
4328+
free(usercontext.apid);
4329+
free(usercontext.ctid);
4330+
free(buffer);
43224331
return -1;
43234332
}
43244333

@@ -4337,6 +4346,9 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
43374346
usercontext.ctid,
43384347
__func__);
43394348

4349+
free(usercontext.apid);
4350+
free(usercontext.ctid);
4351+
free(buffer);
43404352
return 0;
43414353
}
43424354

@@ -4347,6 +4359,9 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
43474359
/* Plausibility check */
43484360
if ((usercontext.log_level < DLT_LOG_DEFAULT) ||
43494361
(usercontext.log_level > DLT_LOG_VERBOSE)) {
4362+
free(usercontext.apid);
4363+
free(usercontext.ctid);
4364+
free(buffer);
43504365
return -1;
43514366
}
43524367
}
@@ -4358,6 +4373,9 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
43584373
/* Plausibility check */
43594374
if ((usercontext.trace_status < DLT_TRACE_STATUS_DEFAULT) ||
43604375
(usercontext.trace_status > DLT_TRACE_STATUS_ON)) {
4376+
free(usercontext.apid);
4377+
free(usercontext.ctid);
4378+
free(buffer);
43614379
return -1;
43624380
}
43634381
}
@@ -4379,6 +4397,9 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
43794397
dlt_vlog(LOG_WARNING,
43804398
"Can't add ContextID '%s' for ApID '%s'\n in %s",
43814399
usercontext.ctid, usercontext.apid, __func__);
4400+
free(usercontext.apid);
4401+
free(usercontext.ctid);
4402+
free(buffer);
43824403
return -1;
43834404
}
43844405
else {
@@ -4483,10 +4504,16 @@ int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
44834504
__func__,
44844505
context->apid,
44854506
context->ctid);
4507+
free(usercontext.apid);
4508+
free(usercontext.ctid);
4509+
free(buffer);
44864510
return -1;
44874511
}
44884512
}
44894513
}
4514+
free(usercontext.apid);
4515+
free(usercontext.ctid);
4516+
free(buffer);
44904517
} else if (daemon->daemon_version == DLTProtocolV1) {
44914518
DltMessage msg;
44924519
DltServiceGetLogInfoRequest *req = NULL;

src/daemon/dlt_daemon_client.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ int dlt_daemon_client_send_message_to_all_client_v2(DltDaemon *daemon,
594594

595595
/* Save old storage header size before we recalculate it */
596596
uint32_t old_storage_size = daemon_local->msgv2.storageheadersizev2;
597-
597+
598598
/* prepare storage header */
599599
if (DLT_IS_HTYP2_WEID(daemon_local->msgv2.baseheaderv2->htyp2)) {
600600
ecu_ptr = daemon_local->msgv2.extendedheaderv2.ecid;
@@ -633,14 +633,14 @@ int dlt_daemon_client_send_message_to_all_client_v2(DltDaemon *daemon,
633633
memcpy(new_headerbufferv2 + daemon_local->msgv2.storageheadersizev2,
634634
temp_buffer + old_storage_size,
635635
(daemon_local->msgv2.baseheadersizev2 + daemon_local->msgv2.baseheaderextrasizev2));
636-
636+
637637
/* Copy extended header from temp buffer */
638638
uint32_t old_extended_offset = old_storage_size + daemon_local->msgv2.baseheadersizev2 + daemon_local->msgv2.baseheaderextrasizev2;
639639
uint32_t new_extended_offset = daemon_local->msgv2.storageheadersizev2 + daemon_local->msgv2.baseheadersizev2 + daemon_local->msgv2.baseheaderextrasizev2;
640640
memcpy(new_headerbufferv2 + new_extended_offset,
641641
temp_buffer + old_extended_offset,
642642
temp_extended_size);
643-
643+
644644
free(temp_buffer);
645645

646646
/* free the original header buffer and install the new one */
@@ -871,7 +871,7 @@ int dlt_daemon_client_send_control_message_v2(int sock,
871871
msg->headerextrav2.seconds[2]=(t >> 16) & 0xFF;
872872
msg->headerextrav2.seconds[3]=(t >> 8) & 0xFF;
873873
msg->headerextrav2.seconds[4]= t & 0xFF;
874-
msg->headerextrav2.nanoseconds |= 0x8000;
874+
msg->headerextrav2.nanoseconds |= 0x80000000;
875875
}
876876
#else
877877
struct timespec ts;
@@ -893,7 +893,7 @@ int dlt_daemon_client_send_control_message_v2(int sock,
893893
if (ts.tv_nsec < 0x3B9ACA00) {
894894
msg->headerextrav2.nanoseconds = (uint32_t) ts.tv_nsec; /* value is long */
895895
}
896-
msg->headerextrav2.nanoseconds |= 0x8000;
896+
msg->headerextrav2.nanoseconds |= 0x80000000;
897897
}
898898
#endif
899899

@@ -955,7 +955,7 @@ int dlt_daemon_client_send_control_message_v2(int sock,
955955
return DLT_RETURN_ERROR;
956956
}
957957

958-
msg->baseheaderv2->len = (uint16_t)len;
958+
msg->baseheaderv2->len = DLT_HTOBE_16((uint16_t)len);
959959

960960
if ((ret =
961961
dlt_daemon_client_send_v2(sock, daemon, daemon_local, msg->headerbufferv2, (int)msg->storageheadersizev2,
@@ -2104,7 +2104,7 @@ void dlt_daemon_control_get_log_info_v2(int sock,
21042104
DltServiceGetLogInfoRequestV2 *req;
21052105
DltMessageV2 resp;
21062106
DltDaemonContext *context = NULL;
2107-
DltDaemonApplication *application = (DltDaemonApplication *)malloc(sizeof(DltDaemonApplication));
2107+
DltDaemonApplication *application = NULL;
21082108

21092109
int num_applications = 0, num_contexts = 0;
21102110
uint16_t count_app_ids = 0, count_con_ids = 0;

src/daemon/dlt_daemon_common.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ DltDaemonContext *dlt_daemon_context_add_v2(DltDaemon *daemon,
18471847
DltDaemonContext *old;
18481848
int new_context = 0;
18491849
DltDaemonRegisteredUsers *user_list = NULL;
1850-
DltDaemonApplication *application = (DltDaemonApplication *)malloc(sizeof(DltDaemonApplication));
1850+
DltDaemonApplication *application = NULL;
18511851

18521852
PRINT_FUNCTION_VERBOSE(verbose);
18531853

@@ -2322,11 +2322,20 @@ int dlt_daemon_contexts_clear(DltDaemon *daemon, char *ecu, int verbose)
23222322
if (users == NULL)
23232323
return DLT_RETURN_ERROR;
23242324

2325-
for (i = 0; i < users->num_contexts; i++)
2325+
for (i = 0; i < users->num_contexts; i++) {
23262326
if (users->contexts[i].context_description != NULL) {
23272327
free(users->contexts[i].context_description);
23282328
users->contexts[i].context_description = NULL;
23292329
}
2330+
if (users->contexts[i].apid2 != NULL) {
2331+
free(users->contexts[i].apid2);
2332+
users->contexts[i].apid2 = NULL;
2333+
}
2334+
if (users->contexts[i].ctid2 != NULL) {
2335+
free(users->contexts[i].ctid2);
2336+
users->contexts[i].ctid2 = NULL;
2337+
}
2338+
}
23302339

23312340
if (users->contexts) {
23322341
free(users->contexts);
@@ -2711,7 +2720,7 @@ int dlt_daemon_user_send_log_level_v2(DltDaemon *daemon, DltDaemonContext *conte
27112720
DltUserHeader userheader;
27122721
DltUserControlMsgLogLevel usercontext;
27132722
DltReturnValue ret;
2714-
DltDaemonApplication *app = (DltDaemonApplication *)malloc(sizeof(DltDaemonApplication));
2723+
DltDaemonApplication *app = NULL;
27152724

27162725
PRINT_FUNCTION_VERBOSE(verbose);
27172726

@@ -2763,7 +2772,6 @@ int dlt_daemon_user_send_log_level_v2(DltDaemon *daemon, DltDaemonContext *conte
27632772
if (app != NULL)
27642773
dlt_daemon_application_reset_user_handle(daemon, app, verbose);
27652774
}
2766-
free(app);
27672775
}
27682776
return (ret == DLT_RETURN_OK) ? DLT_RETURN_OK : DLT_RETURN_ERROR;
27692777
}

src/lib/dlt_client.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ DltReturnValue dlt_client_send_ctrl_msg_v2(DltClient *client, char *apid, char *
10251025
msg.headerextrav2.seconds[2]=(t >> 16) & 0xFF;
10261026
msg.headerextrav2.seconds[3]=(t >> 8) & 0xFF;
10271027
msg.headerextrav2.seconds[4]= t & 0xFF;
1028-
msg.headerextrav2.nanoseconds |= 0x8000;
1028+
msg.headerextrav2.nanoseconds |= 0x80000000;
10291029
}
10301030
#else
10311031
struct timespec ts;
@@ -1047,7 +1047,7 @@ DltReturnValue dlt_client_send_ctrl_msg_v2(DltClient *client, char *apid, char *
10471047
if (ts.tv_nsec < 0x3B9ACA00) {
10481048
msg.headerextrav2.nanoseconds = (uint32_t) ts.tv_nsec; /* value is long */
10491049
}
1050-
msg.headerextrav2.nanoseconds |= 0x8000;
1050+
msg.headerextrav2.nanoseconds |= 0x80000000;
10511051
}
10521052
#endif
10531053

@@ -1108,7 +1108,7 @@ DltReturnValue dlt_client_send_ctrl_msg_v2(DltClient *client, char *apid, char *
11081108
dlt_message_free_v2(&msg, 0);
11091109
return DLT_RETURN_ERROR;
11101110
}
1111-
msg.baseheaderv2->len = (uint16_t)len;
1111+
msg.baseheaderv2->len = DLT_HTOBE_16((uint16_t)len);
11121112

11131113
/* Send data (without storage header) */
11141114
if ((client->mode == DLT_CLIENT_MODE_TCP) || (client->mode == DLT_CLIENT_MODE_SERIAL)) {

src/lib/dlt_user.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5460,7 +5460,7 @@ DltReturnValue dlt_user_log_send_log_v2(DltContextData *log, const int mtype, Dl
54605460
msg.headerextrav2.seconds[2]=(t >> 16) & 0xFF;
54615461
msg.headerextrav2.seconds[3]=(t >> 8) & 0xFF;
54625462
msg.headerextrav2.seconds[4]= t & 0xFF;
5463-
msg.headerextrav2.nanoseconds |= 0x8000;
5463+
msg.headerextrav2.nanoseconds |= 0x80000000;
54645464
}
54655465
#else
54665466
struct timespec ts;
@@ -5482,7 +5482,7 @@ DltReturnValue dlt_user_log_send_log_v2(DltContextData *log, const int mtype, Dl
54825482
if (ts.tv_nsec < 0x3B9ACA00) {
54835483
msg.headerextrav2.nanoseconds = (uint32_t) ts.tv_nsec; /* value is long */
54845484
}
5485-
msg.headerextrav2.nanoseconds |= 0x8000;
5485+
msg.headerextrav2.nanoseconds |= 0x80000000;
54865486
}
54875487
#endif
54885488
}
@@ -5589,7 +5589,7 @@ DltReturnValue dlt_user_log_send_log_v2(DltContextData *log, const int mtype, Dl
55895589
}
55905590
len = (uint32_t)tmplen;
55915591

5592-
msg.baseheaderv2->len = (uint16_t) len;
5592+
msg.baseheaderv2->len = DLT_HTOBE_16((uint16_t) len);
55935593

55945594
/* print to std out, if enabled */
55955595
if ((dlt_user.local_print_mode != DLT_PM_FORCE_OFF) &&
@@ -6522,7 +6522,7 @@ DltReturnValue dlt_user_print_msg_v2(DltMessageV2 *msg, DltContextData *log)
65226522
databuffersize_tmp = msg->databuffersize;
65236523

65246524
/* Act like a receiver, convert header back to host format */
6525-
//msg->baseheaderv2->len = DLT_BETOH_16(msg->baseheaderv2->len);
6525+
msg->baseheaderv2->len = DLT_BETOH_16(msg->baseheaderv2->len);
65266526
//dlt_message_get_storageparameters_v2(msg, 0);
65276527
//dlt_message_get_extraparameters_v2(msg, 0);
65286528

@@ -6542,7 +6542,7 @@ DltReturnValue dlt_user_print_msg_v2(DltMessageV2 *msg, DltContextData *log)
65426542
msg->databuffersize = databuffersize_tmp;
65436543
msg->datasize = datasize_tmp;
65446544

6545-
//msg->baseheaderv2->len = DLT_HTOBE_16(msg->baseheaderv2->len);
6545+
msg->baseheaderv2->len = DLT_HTOBE_16(msg->baseheaderv2->len);
65466546
return DLT_RETURN_OK;
65476547
}
65486548

0 commit comments

Comments
 (0)