From 4199f6896aee4394bddfb85e45eabe34d60038cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 4 Jan 2019 12:37:10 +0100 Subject: [PATCH] Use c99 static array size declarations in exported functions too It seems quite useful to provide this additional information in public exported functions. This is a c99 feature, not supported in C++. Without the check in _sd-common.h: FAILED: test-bus-vtable-cc@exe/src_libsystemd_sd-bus_test-bus-vtable-cc.cc.o ... In file included from ../src/libsystemd/sd-bus/test-bus-vtable-cc.cc:9: In file included from ../src/systemd/sd-bus-vtable.h:26: In file included from ../src/systemd/sd-bus.h:26: ../src/systemd/sd-id128.h:38:47: error: static array size is a C99 feature, not permitted in C++ char *sd_id128_to_string(sd_id128_t id, char s[static SD_ID128_STRING_MAX]); ^ In .c files, I opted to use the define for consistency, even though we don't support compilation with a C++ compiler, so the unconditional keyword would work too. --- src/libsystemd-network/lldp-neighbor.c | 4 ++-- src/libsystemd/sd-id128/sd-id128.c | 2 +- src/systemd/_sd-common.h | 8 ++++++++ src/systemd/sd-id128.h | 2 +- src/systemd/sd-lldp.h | 4 ++-- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c index 43fc8e03c0..f6db62594d 100644 --- a/src/libsystemd-network/lldp-neighbor.c +++ b/src/libsystemd-network/lldp-neighbor.c @@ -691,7 +691,7 @@ _public_ int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor *n, uint8_t type) { return type == k; } -_public_ int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[3], uint8_t *subtype) { +_public_ int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t *subtype) { const uint8_t *d; size_t length; int r; @@ -720,7 +720,7 @@ _public_ int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[3], u return 0; } -_public_ int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[3], uint8_t subtype) { +_public_ int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype) { uint8_t k[3], st; int r; diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index 3593a71c02..e72af1593c 100644 --- a/src/libsystemd/sd-id128/sd-id128.c +++ b/src/libsystemd/sd-id128/sd-id128.c @@ -18,7 +18,7 @@ #include "user-util.h" #include "util.h" -_public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) { +_public_ char *sd_id128_to_string(sd_id128_t id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX]) { unsigned n; assert_return(s, NULL); diff --git a/src/systemd/_sd-common.h b/src/systemd/_sd-common.h index 05c38008cf..b3ee7bbc24 100644 --- a/src/systemd/_sd-common.h +++ b/src/systemd/_sd-common.h @@ -72,6 +72,14 @@ typedef void (*_sd_destroy_t)(void *userdata); # endif #endif +#ifndef _SD_ARRAY_STATIC +# if __STDC_VERSION__ >= 199901L +# define _SD_ARRAY_STATIC static +# else +# define _SD_ARRAY_STATIC +# endif +#endif + #define _SD_DEFINE_POINTER_CLEANUP_FUNC(type, func) \ static __inline__ void func##p(type **p) { \ if (*p) \ diff --git a/src/systemd/sd-id128.h b/src/systemd/sd-id128.h index f4c05a3683..bdf88ed53f 100644 --- a/src/systemd/sd-id128.h +++ b/src/systemd/sd-id128.h @@ -35,7 +35,7 @@ union sd_id128 { #define SD_ID128_STRING_MAX 33 -char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]); +char *sd_id128_to_string(sd_id128_t id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX]); int sd_id128_from_string(const char *s, sd_id128_t *ret); int sd_id128_randomize(sd_id128_t *ret); diff --git a/src/systemd/sd-lldp.h b/src/systemd/sd-lldp.h index a3e5cd6be6..9912f3a22c 100644 --- a/src/systemd/sd-lldp.h +++ b/src/systemd/sd-lldp.h @@ -168,8 +168,8 @@ int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor *n); int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor *n); int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor *n, uint8_t *type); int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor *n, uint8_t type); -int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[3], uint8_t *subtype); -int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[3], uint8_t subtype); +int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t *subtype); +int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype); int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp, sd_lldp_unref);