Skip to content

Commit d687601

Browse files
committed
cleanup(libsinsp): remove misaligned access from sinsp_filtercheck.cpp
Signed-off-by: Luca Guerra <[email protected]>
1 parent 746a94a commit d687601

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

userspace/libsinsp/sinsp_filtercheck.cpp

+23-15
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ void sinsp_filter_check::set_inspector(sinsp* inspector)
7272
m_inspector = inspector;
7373
}
7474

75+
template <class T>
76+
static inline T rawval_cast(uint8_t *rawval)
77+
{
78+
T val;
79+
memcpy(&val, rawval, sizeof(T));
80+
return val;
81+
}
82+
7583
Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval,
7684
ppm_param_type ptype,
7785
ppm_print_format print_format,
@@ -102,7 +110,7 @@ Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval,
102110
if(print_format == PF_DEC ||
103111
print_format == PF_ID)
104112
{
105-
return *(int16_t *)rawval;
113+
return rawval_cast<int16_t>(rawval);
106114
}
107115
else if(print_format == PF_OCT ||
108116
print_format == PF_HEX)
@@ -119,7 +127,7 @@ Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval,
119127
if(print_format == PF_DEC ||
120128
print_format == PF_ID)
121129
{
122-
return *(int32_t *)rawval;
130+
return rawval_cast<int32_t>(rawval);
123131
}
124132
else if(print_format == PF_OCT ||
125133
print_format == PF_HEX)
@@ -134,19 +142,19 @@ Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval,
134142
case PT_DOUBLE:
135143
if(print_format == PF_DEC)
136144
{
137-
return (Json::Value::Int64)(int64_t)*(double*)rawval;
145+
return (Json::Value::Int64)(int64_t)rawval_cast<double>(rawval);
138146
}
139147
else
140148
{
141-
return (Json::Value)*(double*)rawval;
149+
return (Json::Value)rawval_cast<double>(rawval);
142150
}
143151
case PT_INT64:
144152
case PT_PID:
145153
case PT_FD:
146154
if(print_format == PF_DEC ||
147155
print_format == PF_ID)
148156
{
149-
return (Json::Value::Int64)*(int64_t *)rawval;
157+
return (Json::Value::Int64)rawval_cast<int64_t>(rawval);
150158
}
151159
else
152160
{
@@ -212,7 +220,7 @@ Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval,
212220
if(print_format == PF_DEC ||
213221
print_format == PF_ID)
214222
{
215-
return (Json::Value::UInt64)*(uint64_t *)rawval;
223+
return (Json::Value::UInt64)rawval_cast<uint64_t>(rawval);
216224
}
217225
else if(
218226
print_format == PF_10_PADDED_DEC ||
@@ -233,7 +241,7 @@ Json::Value sinsp_filter_check::rawval_to_json(uint8_t* rawval,
233241
return Json::nullValue;
234242

235243
case PT_BOOL:
236-
return Json::Value((bool)(*(uint32_t*)rawval != 0));
244+
return Json::Value((bool)(rawval_cast<uint32_t>(rawval) != 0));
237245

238246
case PT_CHARBUF:
239247
case PT_FSPATH:
@@ -309,7 +317,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
309317
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
310318
snprintf(m_getpropertystr_storage.data(),
311319
STRPROPERTY_STORAGE_SIZE,
312-
prfmt, *(int16_t *)rawval);
320+
prfmt, rawval_cast<int16_t>(rawval));
313321
return m_getpropertystr_storage.data();
314322
case PT_INT32:
315323
if(print_format == PF_OCT)
@@ -334,7 +342,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
334342
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
335343
snprintf(m_getpropertystr_storage.data(),
336344
STRPROPERTY_STORAGE_SIZE,
337-
prfmt, *(int32_t *)rawval);
345+
prfmt, rawval_cast<int32_t>(rawval));
338346
return m_getpropertystr_storage.data();
339347
case PT_INT64:
340348
case PT_PID:
@@ -365,7 +373,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
365373
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
366374
snprintf(m_getpropertystr_storage.data(),
367375
STRPROPERTY_STORAGE_SIZE,
368-
prfmt, *(int64_t *)rawval);
376+
prfmt, rawval_cast<int64_t>(rawval));
369377
return m_getpropertystr_storage.data();
370378
case PT_L4PROTO: // This can be resolved in the future
371379
case PT_UINT8:
@@ -417,7 +425,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
417425
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
418426
snprintf(m_getpropertystr_storage.data(),
419427
STRPROPERTY_STORAGE_SIZE,
420-
prfmt, *(uint16_t *)rawval);
428+
prfmt, rawval_cast<uint16_t>(rawval));
421429
return m_getpropertystr_storage.data();
422430
case PT_UINT32:
423431
if(print_format == PF_OCT)
@@ -442,7 +450,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
442450
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
443451
snprintf(m_getpropertystr_storage.data(),
444452
STRPROPERTY_STORAGE_SIZE,
445-
prfmt, *(uint32_t *)rawval);
453+
prfmt, rawval_cast<uint32_t>(rawval));
446454
return m_getpropertystr_storage.data();
447455
case PT_UINT64:
448456
case PT_RELTIME:
@@ -473,7 +481,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
473481
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
474482
snprintf(m_getpropertystr_storage.data(),
475483
STRPROPERTY_STORAGE_SIZE,
476-
prfmt, *(uint64_t *)rawval);
484+
prfmt, rawval_cast<uint64_t>(rawval));
477485
return m_getpropertystr_storage.data();
478486
case PT_CHARBUF:
479487
case PT_FSPATH:
@@ -494,7 +502,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
494502
ASSERT(false);
495503
return NULL;
496504
case PT_BOOL:
497-
if(*(uint32_t*)rawval != 0)
505+
if(rawval_cast<uint32_t>(rawval) != 0)
498506
{
499507
return (char*)"true";
500508
}
@@ -544,7 +552,7 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
544552
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);
545553
snprintf(m_getpropertystr_storage.data(),
546554
STRPROPERTY_STORAGE_SIZE,
547-
"%.1lf", *(double*)rawval);
555+
"%.1lf", rawval_cast<double>(rawval));
548556
return m_getpropertystr_storage.data();
549557
case PT_IPNET:
550558
m_getpropertystr_storage.resize(STRPROPERTY_STORAGE_SIZE);

userspace/libsinsp/sinsp_filtercheck_event.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,7 @@ uint8_t* sinsp_filter_check_event::extract_single(sinsp_evt *evt, uint32_t* len,
12471247

12481248
if(pi != NULL)
12491249
{
1250-
ASSERT(pi->m_len == sizeof(int64_t));
1251-
if(*(int64_t*)pi->m_val < 0)
1250+
if(pi->as<int64_t>() < 0)
12521251
{
12531252
m_val.u32 = 1;
12541253
}
@@ -1259,8 +1258,7 @@ uint8_t* sinsp_filter_check_event::extract_single(sinsp_evt *evt, uint32_t* len,
12591258

12601259
if(pi != NULL)
12611260
{
1262-
ASSERT(pi->m_len == sizeof(int64_t));
1263-
if(*(int64_t*)pi->m_val < 0)
1261+
if(pi->as<int64_t>() < 0)
12641262
{
12651263
m_val.u32 = 1;
12661264
}

0 commit comments

Comments
 (0)