Skip to content

Commit fa3331f

Browse files
Fix UB in array related methods (#313) (#314)
* Refs #24365: Fix UB in calculate_array_serialized_size * Refs #24365: Fix UB in serialize array * Refs #24365: Unrelated Uncrustify * Refs #24365: Revision * Refs #24365: Unrelated Uncrustify FastCdr.h --------- (cherry picked from commit a9bd70c) Signed-off-by: Carlos Ferreira González <carlosferreira@eprosima.com> Co-authored-by: Carlos Ferreira González <carlosferreira@eprosima.com>
1 parent c9d5858 commit fa3331f

3 files changed

Lines changed: 56 additions & 18 deletions

File tree

include/fastcdr/Cdr.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ class Cdr
720720
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
721721
* position that exceeds the internal memory size.
722722
*/
723-
template <size_t MAX_CHARS>
723+
template<size_t MAX_CHARS>
724724
Cdr& serialize(
725725
const fixed_string<MAX_CHARS>& value)
726726
{
@@ -1787,7 +1787,7 @@ class Cdr
17871787
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
17881788
* position that exceeds the internal memory size.
17891789
*/
1790-
template <size_t MAX_CHARS>
1790+
template<size_t MAX_CHARS>
17911791
Cdr& deserialize(
17921792
fixed_string<MAX_CHARS>& value)
17931793
{
@@ -2973,6 +2973,10 @@ class Cdr
29732973
const std::array<_T, _Size>* array_t,
29742974
size_t num_elements)
29752975
{
2976+
if (num_elements == 0 || array_t == nullptr)
2977+
{
2978+
return *this;
2979+
}
29762980
return serialize_array(array_t->data(), num_elements * array_t->size());
29772981
}
29782982

@@ -2988,6 +2992,10 @@ class Cdr
29882992
std::array<_T, _Size>* array_t,
29892993
size_t num_elements)
29902994
{
2995+
if (num_elements == 0 || array_t == nullptr)
2996+
{
2997+
return *this;
2998+
}
29912999
return deserialize_array(array_t->data(), num_elements * array_t->size());
29923000
}
29933001

@@ -3005,6 +3013,10 @@ class Cdr
30053013
size_t num_elements,
30063014
Endianness endianness)
30073015
{
3016+
if (num_elements == 0 || array_t == nullptr)
3017+
{
3018+
return *this;
3019+
}
30083020
return deserialize_array(array_t->data(), num_elements * array_t->size(), endianness);
30093021
}
30103022

include/fastcdr/CdrSizeCalculator.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ class CdrSizeCalculator
480480
* @param[inout] current_alignment Current alignment in the encoding.
481481
* @return Encoded size of the instance.
482482
*/
483-
template <size_t MAX_CHARS>
483+
template<size_t MAX_CHARS>
484484
size_t calculate_serialized_size(
485485
const fixed_string<MAX_CHARS>& data,
486486
size_t& current_alignment)
@@ -1065,7 +1065,11 @@ class CdrSizeCalculator
10651065
size_t num_elements,
10661066
size_t& current_alignment)
10671067
{
1068-
return calculate_array_serialized_size(data->data(), num_elements * data->size(), current_alignment);
1068+
if (num_elements == 0 || data == nullptr)
1069+
{
1070+
return 0;
1071+
}
1072+
return calculate_array_serialized_size(data->data(), num_elements * _N, current_alignment);
10691073
}
10701074

10711075
/*!

include/fastcdr/FastCdr.h

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ class Cdr_DllAPI FastCdr
655655
return *this;
656656
}
657657

658-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
658+
throw exception::NotEnoughMemoryException(
659+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
659660
}
660661

661662
/*!
@@ -702,7 +703,8 @@ class Cdr_DllAPI FastCdr
702703
return *this;
703704
}
704705

705-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
706+
throw exception::NotEnoughMemoryException(
707+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
706708
}
707709

708710
/*!
@@ -736,7 +738,8 @@ class Cdr_DllAPI FastCdr
736738
return *this;
737739
}
738740

739-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
741+
throw exception::NotEnoughMemoryException(
742+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
740743
}
741744

742745
/*!
@@ -783,7 +786,8 @@ class Cdr_DllAPI FastCdr
783786
return *this;
784787
}
785788

786-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
789+
throw exception::NotEnoughMemoryException(
790+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
787791
}
788792

789793
/*!
@@ -804,7 +808,8 @@ class Cdr_DllAPI FastCdr
804808
return *this;
805809
}
806810

807-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
811+
throw exception::NotEnoughMemoryException(
812+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
808813
}
809814

810815
/*!
@@ -825,7 +830,8 @@ class Cdr_DllAPI FastCdr
825830
return *this;
826831
}
827832

828-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
833+
throw exception::NotEnoughMemoryException(
834+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
829835
}
830836

831837
/*!
@@ -850,7 +856,8 @@ class Cdr_DllAPI FastCdr
850856
return *this;
851857
}
852858

853-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
859+
throw exception::NotEnoughMemoryException(
860+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
854861
}
855862

856863
/*!
@@ -1306,7 +1313,8 @@ class Cdr_DllAPI FastCdr
13061313
return *this;
13071314
}
13081315

1309-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1316+
throw exception::NotEnoughMemoryException(
1317+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
13101318
}
13111319

13121320
/*!
@@ -1353,7 +1361,8 @@ class Cdr_DllAPI FastCdr
13531361
return *this;
13541362
}
13551363

1356-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1364+
throw exception::NotEnoughMemoryException(
1365+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
13571366
}
13581367

13591368
/*!
@@ -1387,7 +1396,8 @@ class Cdr_DllAPI FastCdr
13871396
return *this;
13881397
}
13891398

1390-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1399+
throw exception::NotEnoughMemoryException(
1400+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
13911401
}
13921402

13931403
/*!
@@ -1437,7 +1447,8 @@ class Cdr_DllAPI FastCdr
14371447
return *this;
14381448
}
14391449

1440-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1450+
throw exception::NotEnoughMemoryException(
1451+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
14411452
}
14421453

14431454
/*!
@@ -1458,7 +1469,8 @@ class Cdr_DllAPI FastCdr
14581469
return *this;
14591470
}
14601471

1461-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1472+
throw exception::NotEnoughMemoryException(
1473+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
14621474
}
14631475

14641476
/*!
@@ -1479,7 +1491,8 @@ class Cdr_DllAPI FastCdr
14791491
return *this;
14801492
}
14811493

1482-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1494+
throw exception::NotEnoughMemoryException(
1495+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
14831496
}
14841497

14851498
/*!
@@ -1503,7 +1516,8 @@ class Cdr_DllAPI FastCdr
15031516
return *this;
15041517
}
15051518

1506-
throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1519+
throw exception::NotEnoughMemoryException(
1520+
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
15071521
}
15081522

15091523
/*!
@@ -2047,6 +2061,10 @@ class Cdr_DllAPI FastCdr
20472061
const std::array<_T, _Size>* array_t,
20482062
size_t num_elements)
20492063
{
2064+
if (num_elements == 0 || array_t == nullptr)
2065+
{
2066+
return *this;
2067+
}
20502068
return serialize_array(array_t->data(), num_elements * array_t->size());
20512069
}
20522070

@@ -2062,6 +2080,10 @@ class Cdr_DllAPI FastCdr
20622080
std::array<_T, _Size>* array_t,
20632081
size_t num_elements)
20642082
{
2083+
if (num_elements == 0 || array_t == nullptr)
2084+
{
2085+
return *this;
2086+
}
20652087
return deserialize_array(array_t->data(), num_elements * array_t->size());
20662088
}
20672089

0 commit comments

Comments
 (0)