Merge pull request #7625 from thom311/th/const-strlen

Don't use strlen() to declare variable-length arrays
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-12-14 09:41:09 +01:00 committed by GitHub
commit 966c04cf01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View File

@ -368,7 +368,7 @@ bool fdname_is_valid(const char *s) {
}
int fd_get_path(int fd, char **ret) {
char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
int r;
xsprintf(procfs_path, "/proc/self/fd/%i", fd);

View File

@ -144,6 +144,14 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
!__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
sizeof(x)/sizeof((x)[0]), \
(void)0))
/*
* STRLEN - return the length of a string literal, minus the trailing NUL byte.
* Contrary to strlen(), this is a constant expression.
* @x: a string literal.
*/
#define STRLEN(x) (sizeof(""x"") - 1)
/*
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.

View File

@ -987,7 +987,7 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
}
LIST_FOREACH(options, option, lease->private_options) {
char key[strlen("OPTION_000")+1];
char key[STRLEN("OPTION_000")+1];
xsprintf(key, "OPTION_%" PRIu8, option->tag);
r = serialize_dhcp_option(f, key, option->data, option->length);