Merge pull request #18557 from poettering/enum-force-s64
force public enums to be 64bit wide
This commit is contained in:
@@ -40,10 +40,16 @@ int main(int argc, char **argv) {
|
||||
test_table(wol, WOL);
|
||||
test_table(lldp_event, SD_LLDP_EVENT);
|
||||
test_table(ndisc_event, SD_NDISC_EVENT);
|
||||
test_table(dhcp_lease_server_type, SD_DHCP_LEASE_SERVER_TYPE);
|
||||
|
||||
test_table_sparse(ipvlan_mode, NETDEV_IPVLAN_MODE);
|
||||
test_table_sparse(macvlan_mode, NETDEV_MACVLAN_MODE);
|
||||
test_table_sparse(address_family, ADDRESS_FAMILY);
|
||||
|
||||
assert_cc(sizeof(sd_lldp_event) == sizeof(int64_t));
|
||||
assert_cc(sizeof(sd_ndisc_event) == sizeof(int64_t));
|
||||
assert_cc(sizeof(sd_dhcp_lease_server_type) == sizeof(int64_t));
|
||||
assert_cc(sizeof(sd_genl_family) == sizeof(int64_t));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
+31
-36
@@ -1,48 +1,43 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef const char* (*lookup_t)(int);
|
||||
typedef int (*reverse_t)(const char*);
|
||||
#include "string-util.h"
|
||||
|
||||
static inline void _test_table(const char *name,
|
||||
lookup_t lookup,
|
||||
reverse_t reverse,
|
||||
int size,
|
||||
bool sparse) {
|
||||
int i, boring = 0;
|
||||
|
||||
for (i = -1; i < size + 1; i++) {
|
||||
const char* val = lookup(i);
|
||||
int rev;
|
||||
|
||||
if (val) {
|
||||
rev = reverse(val);
|
||||
boring = 0;
|
||||
} else {
|
||||
rev = reverse("--no-such--value----");
|
||||
boring += i >= 0;
|
||||
}
|
||||
|
||||
if (boring < 1 || i == size)
|
||||
printf("%s: %d → %s → %d\n", name, i, strnull(val), rev);
|
||||
else if (boring == 1)
|
||||
printf("%*s ...\n", (int) strlen(name), "");
|
||||
|
||||
if (i >= 0 && i < size) {
|
||||
if (sparse)
|
||||
assert_se(rev == i || rev == -EINVAL);
|
||||
else
|
||||
assert_se(val != NULL && rev == i);
|
||||
} else
|
||||
assert_se(val == NULL && rev == -EINVAL);
|
||||
#define _test_table(name, lookup, reverse, size, sparse) \
|
||||
for (int64_t _i = -EINVAL, _boring = 0; _i < size + 1; _i++) { \
|
||||
const char* _val; \
|
||||
int64_t _rev; \
|
||||
\
|
||||
_val = lookup(_i); \
|
||||
if (_val) { \
|
||||
_rev = reverse(_val); \
|
||||
_boring = 0; \
|
||||
} else { \
|
||||
_rev = reverse("--no-such--value----"); \
|
||||
_boring += _i >= 0; \
|
||||
} \
|
||||
if (_boring == 0 || _i == size) \
|
||||
printf("%s: %" PRIi64 " → %s → %" PRIi64 "\n", name, _i, strnull(_val), _rev); \
|
||||
else if (_boring == 1) \
|
||||
printf("%*s ...\n", (int) strlen(name), ""); \
|
||||
\
|
||||
if (_i >= 0 && _i < size) { \
|
||||
if (sparse) \
|
||||
assert_se(_rev == _i || _rev == -EINVAL); \
|
||||
else \
|
||||
assert_se(_val && _rev == _i); \
|
||||
} else \
|
||||
assert_se(!_val && _rev == -EINVAL); \
|
||||
}
|
||||
}
|
||||
|
||||
#define test_table(lower, upper) \
|
||||
#define test_table(lower, upper) \
|
||||
_test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
|
||||
|
||||
#define test_table_sparse(lower, upper) \
|
||||
#define test_table_sparse(lower, upper) \
|
||||
_test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)
|
||||
|
||||
@@ -42,7 +42,7 @@ int wifi_get_interface(sd_netlink *genl, int ifindex, enum nl80211_iftype *iftyp
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to determine genl family: %m");
|
||||
if (family != SD_GENL_NL80211) {
|
||||
log_debug("Received message of unexpected genl family %u, ignoring.", family);
|
||||
log_debug("Received message of unexpected genl family %" PRIi64 ", ignoring.", family);
|
||||
goto nodata;
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ int wifi_get_station(sd_netlink *genl, int ifindex, struct ether_addr *bssid) {
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to determine genl family: %m");
|
||||
if (family != SD_GENL_NL80211) {
|
||||
log_debug("Received message of unexpected genl family %u, ignoring.", family);
|
||||
log_debug("Received message of unexpected genl family %" PRIi64 ", ignoring.", family);
|
||||
goto nodata;
|
||||
}
|
||||
|
||||
|
||||
@@ -99,4 +99,10 @@ typedef void (*_sd_destroy_t)(void *userdata);
|
||||
} \
|
||||
struct _sd_useless_struct_to_allow_trailing_semicolon_
|
||||
|
||||
/* The following macro should be used in all public enums, to force 64bit wideness on them, so that we can
|
||||
* freely extend them later on, without breaking compatibility. */
|
||||
#define _SD_ENUM_FORCE_S64(id) \
|
||||
_SD_##id##_INT64_MIN = INT64_MIN, \
|
||||
_SD_##id##_INT64_MAX = INT64_MAX
|
||||
|
||||
#endif
|
||||
|
||||
@@ -43,6 +43,7 @@ typedef enum sd_dhcp_lease_server_type {
|
||||
SD_DHCP_LEASE_LPR,
|
||||
_SD_DHCP_LEASE_SERVER_TYPE_MAX,
|
||||
_SD_DHCP_LEASE_SERVER_TYPE_INVALID = -EINVAL,
|
||||
_SD_ENUM_FORCE_S64(DHCP_LEASE_SERVER_TYPE),
|
||||
} sd_dhcp_lease_server_type;
|
||||
|
||||
int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr);
|
||||
|
||||
@@ -129,6 +129,7 @@ typedef enum sd_lldp_event {
|
||||
SD_LLDP_EVENT_REFRESHED,
|
||||
_SD_LLDP_EVENT_MAX,
|
||||
_SD_LLDP_EVENT_INVALID = -EINVAL,
|
||||
_SD_ENUM_FORCE_S64(LLDP_EVENT),
|
||||
} sd_lldp_event;
|
||||
|
||||
typedef void (*sd_lldp_callback_t)(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata);
|
||||
|
||||
@@ -60,6 +60,7 @@ typedef enum sd_ndisc_event {
|
||||
SD_NDISC_EVENT_ROUTER,
|
||||
_SD_NDISC_EVENT_MAX,
|
||||
_SD_NDISC_EVENT_INVALID = -EINVAL,
|
||||
_SD_ENUM_FORCE_S64(NDISC_EVENT),
|
||||
} sd_ndisc_event;
|
||||
|
||||
typedef void (*sd_ndisc_callback_t)(sd_ndisc *nd, sd_ndisc_event event, sd_ndisc_router *rt, void *userdata);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -34,7 +35,7 @@ typedef struct sd_genl_socket sd_genl_socket;
|
||||
typedef struct sd_netlink_message sd_netlink_message;
|
||||
typedef struct sd_netlink_slot sd_netlink_slot;
|
||||
|
||||
typedef enum sd_gen_family {
|
||||
typedef enum sd_genl_family {
|
||||
SD_GENL_ERROR,
|
||||
SD_GENL_DONE,
|
||||
SD_GENL_ID_CTRL,
|
||||
@@ -43,6 +44,9 @@ typedef enum sd_gen_family {
|
||||
SD_GENL_L2TP,
|
||||
SD_GENL_MACSEC,
|
||||
SD_GENL_NL80211,
|
||||
_SD_GENL_FAMILY_MAX,
|
||||
_SD_GENL_FAMILY_INVALID = -EINVAL,
|
||||
_SD_ENUM_FORCE_S64(GENL_FAMILY)
|
||||
} sd_genl_family;
|
||||
|
||||
/* callback */
|
||||
|
||||
Reference in New Issue
Block a user