Skip to content

Commit 3d9f5ef

Browse files
authored
Don't call memory.deallocate for NULL memory (#59)
Also: - Latest `toolshed:ts22.4.3` -> `22.4.10` : gcc 11.3 -> 12.3 - Disable some new clang-tidies
1 parent 6c7c12e commit 3d9f5ef

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Checks: >-
2222
-hicpp-static-assert,
2323
-misc-static-assert,
2424
-modernize-macro-to-enum,
25+
-cppcoreguidelines-macro-to-enum,
26+
-bugprone-casting-through-void,
27+
-misc-include-cleaner,
28+
-cppcoreguidelines-avoid-do-while,
2529
CheckOptions:
2630
- key: readability-function-cognitive-complexity.Threshold
2731
value: '99'

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
debug:
77
if: github.event_name == 'push'
88
runs-on: ubuntu-latest
9-
container: ghcr.io/opencyphal/toolshed:ts22.4.3
9+
container: ghcr.io/opencyphal/toolshed:ts22.4.10
1010
strategy:
1111
matrix:
1212
toolchain: [ 'clang', 'gcc' ]
@@ -46,7 +46,7 @@ jobs:
4646
optimizations:
4747
if: github.event_name == 'push'
4848
runs-on: ubuntu-latest
49-
container: ghcr.io/opencyphal/toolshed:ts22.4.3
49+
container: ghcr.io/opencyphal/toolshed:ts22.4.10
5050
strategy:
5151
matrix:
5252
toolchain: [ 'clang', 'gcc' ]
@@ -55,7 +55,7 @@ jobs:
5555
- toolchain: gcc
5656
c-compiler: gcc
5757
cxx-compiler: g++
58-
cxx-flags: -fno-strict-aliasing # GCC in MinSizeRel C++20 mode misoptimizes the Cavl test.
58+
cxx-flags: -fno-strict-aliasing # GCC in MinSizeRel C++20 mode misoptimizes the Cavl test.
5959
- toolchain: clang
6060
c-compiler: clang
6161
cxx-compiler: clang++
@@ -123,7 +123,7 @@ jobs:
123123

124124
sonar:
125125
runs-on: ubuntu-latest
126-
container: ghcr.io/opencyphal/toolshed:ts22.4.3
126+
container: ghcr.io/opencyphal/toolshed:ts22.4.10
127127
if: >
128128
(
129129
(github.event_name == 'pull_request' || contains(github.ref, '/main') || contains(github.ref, '/release')) &&

libudpard/udpard.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ static inline void memFree(const struct UdpardMemoryResource memory, const size_
140140
static inline void memFreePayload(const struct UdpardMemoryDeleter memory, const struct UdpardMutablePayload payload)
141141
{
142142
UDPARD_ASSERT(memory.deallocate != NULL);
143-
memory.deallocate(memory.user_reference, payload.size, payload.data);
143+
if (payload.data != NULL)
144+
{
145+
memory.deallocate(memory.user_reference, payload.size, payload.data);
146+
}
144147
}
145148

146149
static inline void memZero(const size_t size, void* const data)
@@ -1081,13 +1084,18 @@ static inline bool rxSlotEject(size_t* const out_payload_size,
10811084
{
10821085
result = true;
10831086
*out_payload_size = eject_ctx.retain_size;
1084-
*out_payload_head = (eject_ctx.head != NULL)
1085-
? (*eject_ctx.head) // Slice off the derived type fields as they are not needed.
1086-
: (struct UdpardFragment){.next = NULL, .view = {0, NULL}, .origin = {0, NULL}};
1087-
// This is the single-frame transfer optimization suggested by Scott: we free the first fragment handle
1088-
// early by moving the contents into the rx_transfer structure by value.
1089-
// No need to free the payload buffer because it has been transferred to the transfer.
1090-
memFree(memory.fragment, sizeof(RxFragment), eject_ctx.head); // May be empty.
1087+
if (eject_ctx.head != NULL)
1088+
{
1089+
// This is the single-frame transfer optimization suggested by Scott: we free the first fragment handle
1090+
// early by moving the contents into the rx_transfer structure by value.
1091+
// No need to free the payload buffer because it has been transferred to the transfer.
1092+
*out_payload_head = *eject_ctx.head; // Slice off the derived type fields as they are not needed.
1093+
memFree(memory.fragment, sizeof(RxFragment), eject_ctx.head);
1094+
}
1095+
else
1096+
{
1097+
*out_payload_head = (struct UdpardFragment){.next = NULL, .view = {0, NULL}, .origin = {0, NULL}};
1098+
}
10911099
}
10921100
else // The transfer turned out to be invalid. We have to free the fragments. Can't use the tree anymore.
10931101
{

tests/.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Checks: >-
4141
-modernize-macro-to-enum,
4242
-modernize-use-trailing-return-type,
4343
-cppcoreguidelines-owning-memory,
44+
-misc-include-cleaner,
45+
-performance-avoid-endl,
46+
-cppcoreguidelines-avoid-do-while,
4447
WarningsAsErrors: '*'
4548
HeaderFilterRegex: '.*\.hpp'
4649
FormatStyle: file

0 commit comments

Comments
 (0)