Skip to content

Commit 595fe95

Browse files
authored
Merge pull request #432 from nqminds/tests/fix-test-edgesec-codecov
Use `__maybe_unused` compiler attribute to fix flakey code-coverage in `test_edgesec`
2 parents ef1a558 + f770f44 commit 595fe95

File tree

13 files changed

+72
-33
lines changed

13 files changed

+72
-33
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ set_target_properties(edgesec PROPERTIES
6767

6868
if (USE_CAPTURE_SERVICE)
6969
add_executable(edgesec-recap edgesec-recap.c)
70-
target_link_libraries(edgesec-recap PRIVATE capture_service protobuf_middleware packet_queue packet_decoder sqlite_header os log SQLite::SQLite3 eloop::eloop)
70+
target_link_libraries(edgesec-recap PRIVATE capture_service protobuf_middleware packet_queue packet_decoder sqlite_header attributes os log SQLite::SQLite3 eloop::eloop)
7171
target_include_directories(edgesec-recap PRIVATE ${PROJECT_BINARY_DIR})
7272
endif()

src/capture/middlewares/header_middleware/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ target_link_libraries(dns_decoder PUBLIC PCAP::pcap SQLite::SQLite3 PRIVATE log
1212

1313
# packet_decoder.h has an #include <pcap.h>, so need to make it PUBLIC include
1414
add_library(packet_decoder packet_decoder.c)
15-
target_link_libraries(packet_decoder PUBLIC PCAP::pcap LibUTHash::LibUTHash PRIVATE mdns_decoder dns_decoder hash net log os hashmap)
15+
target_link_libraries(packet_decoder PUBLIC PCAP::pcap LibUTHash::LibUTHash attributes PRIVATE mdns_decoder dns_decoder hash net log os hashmap)
1616

1717
add_library(packet_queue packet_queue.c)
1818
target_link_libraries(packet_queue PUBLIC packet_decoder eloop::list PRIVATE log os)

src/capture/middlewares/header_middleware/packet_decoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <utarray.h>
1818
#include "../../../utils/allocs.h"
19+
#include "../../../utils/attributes.h"
1920
#include "../../../utils/net.h"
2021
#include "../../../utils/os.h"
2122

src/edgesec-recap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "capture/middlewares/header_middleware/packet_queue.h"
3131
#include "capture/middlewares/header_middleware/sqlite_header.h"
3232
#include "capture/middlewares/protobuf_middleware/protobuf_middleware.h"
33+
#include "utils/attributes.h"
3334
#include "utils/os.h"
3435
#include "utils/sqliteu.h"
3536
#include "version.h"

src/radius/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@ include_directories (
22
"${PROJECT_SOURCE_DIR}/src"
33
)
44

5+
add_library(common INTERFACE)
6+
set_target_properties(common PROPERTIES PUBLIC_HEADER "common.h")
7+
target_link_libraries(common INTERFACE attributes allocs log)
8+
59
add_library(md5_internal md5_internal.c)
610
target_link_libraries(md5_internal PRIVATE log os)
711

812
add_library(md5 md5.c)
913
target_link_libraries(md5 PRIVATE md5_internal os)
1014

1115
add_library(wpabuf wpabuf.c)
12-
target_link_libraries(wpabuf PRIVATE log os)
16+
target_link_libraries(wpabuf PUBLIC common PRIVATE log os)
1317
# wpabuf.h has BSD functions like be16toh, see https://linux.die.net/man/3/be16toh
1418
target_compile_definitions(wpabuf PUBLIC _DEFAULT_SOURCE _BSD_SOURCE)
1519

1620
add_library(radius radius.c)
17-
target_link_libraries(radius PRIVATE wpabuf md5 md5_internal log os)
21+
target_link_libraries(radius
22+
PUBLIC common attributes
23+
PRIVATE wpabuf md5 md5_internal log os)
1824

1925
add_library(radius_server radius_server.c)
2026
target_link_libraries(radius_server PUBLIC os eloop::eloop PRIVATE radius wpabuf log net)

src/radius/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <stddef.h>
1717

18+
#include "../utils/attributes.h"
1819
#include "utils/allocs.h"
1920
#include "utils/log.h"
2021

src/radius/radius.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "common.h"
1818

19+
#include "../utils/attributes.h"
1920
#include "utils/os.h"
2021

2122
/* RFC 2865 - RADIUS */

src/utils/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set_target_properties(log PROPERTIES C_EXTENSIONS ON POSITION_INDEPENDENT_CODE O
44

55
add_library(allocs allocs.c)
66

7+
add_library(attributes INTERFACE) # #define's for compiler attributes
8+
set_target_properties(attributes PROPERTIES PUBLIC_HEADER "attributes.h")
9+
710
if (USE_CRYPTO_SERVICE)
811
add_library(cryptou cryptou.c)
912
target_link_libraries(cryptou PRIVATE base64 os log OpenSSL::Crypto)

src/utils/attributes.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file
3+
* @author Alois Klink <[email protected]>
4+
* @date 2023
5+
* @copyright
6+
* SPDX-FileCopyrightText: © 2023 NQMCyber Ltd and edgesec contributors
7+
* SPDX-License-Identifier: Expat
8+
* @brief File containing macros for compiler attributes, if they are supported.
9+
*
10+
* In the future, once we support C23, we can remove this header and just
11+
* use C23 attributes.
12+
*/
13+
#ifndef ATTRIBUTES_H
14+
#define ATTRIBUTES_H
15+
16+
#ifndef __maybe_unused
17+
#if defined __has_attribute
18+
#if __has_attribute(unused)
19+
/**
20+
* If used before a variable, tells the compiler that variable can be unused.
21+
* (e.g. does the same thing as casting to `(void)`, or `[[maybe_unused]]` in
22+
* C23).
23+
*
24+
* @see https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
25+
*/
26+
#define __maybe_unused __attribute__((unused))
27+
#else
28+
#define __maybe_unused
29+
#endif /* __has_attribute(unused) */
30+
#else
31+
#define __maybe_unused
32+
#endif /* defined __has_attribute */
33+
#endif /* __maybe_unused */
34+
35+
#ifdef __GNUC__
36+
#define STRUCT_PACKED __attribute__((packed))
37+
#else
38+
#define STRUCT_PACKED
39+
#endif
40+
41+
#endif /* ATTRIBUTES_H */

src/utils/os.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232

3333
#define OS_HOST_NAME_MAX 64
3434

35-
#ifdef __GNUC__
36-
#define STRUCT_PACKED __attribute__((packed))
37-
#else
38-
#define STRUCT_PACKED
39-
#endif
40-
4135
#ifndef BIT
4236
#define BIT(x) (1U << (x))
4337
#endif

0 commit comments

Comments
 (0)