Revert "lib: fix ubsan errors in cbvprintf_package"
This reverts commit 6f95a50553
.
Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
This commit is contained in:
parent
4793e00ae8
commit
b0f6f1d33a
1 changed files with 52 additions and 53 deletions
|
@ -242,10 +242,10 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
#define STR_POS_MASK BIT_MASK(7)
|
#define STR_POS_MASK BIT_MASK(7)
|
||||||
|
|
||||||
/* Buffer offset abstraction for better code clarity. */
|
/* Buffer offset abstraction for better code clarity. */
|
||||||
#define BUF_OFFSET offset
|
#define BUF_OFFSET ((uintptr_t)buf - (uintptr_t)buf0)
|
||||||
|
|
||||||
uint8_t *buf = packaged; /* buffer start (may be NULL) */
|
uint8_t *buf0 = packaged; /* buffer start (may be NULL) */
|
||||||
size_t offset = 0; /* current buffer position */
|
uint8_t *buf = buf0; /* current buffer position */
|
||||||
unsigned int size; /* current argument's size */
|
unsigned int size; /* current argument's size */
|
||||||
unsigned int align; /* current argument's required alignment */
|
unsigned int align; /* current argument's required alignment */
|
||||||
uint8_t str_ptr_pos[16]; /* string pointer positions */
|
uint8_t str_ptr_pos[16]; /* string pointer positions */
|
||||||
|
@ -294,16 +294,16 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
*
|
*
|
||||||
* Refer to union cbprintf_package_hdr for more details.
|
* Refer to union cbprintf_package_hdr for more details.
|
||||||
*/
|
*/
|
||||||
offset += sizeof(*pkg_hdr);
|
buf += sizeof(*pkg_hdr);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* When buf is NULL we don't store anything.
|
* When buf0 is NULL we don't store anything.
|
||||||
* Instead we count the needed space to store the data.
|
* Instead we count the needed space to store the data.
|
||||||
* In this case, incoming len argument indicates the anticipated
|
* In this case, incoming len argument indicates the anticipated
|
||||||
* buffer "misalignment" offset.
|
* buffer "misalignment" offset.
|
||||||
*/
|
*/
|
||||||
if (buf == NULL) {
|
if (buf0 == NULL) {
|
||||||
offset += len % CBPRINTF_PACKAGE_ALIGNMENT;
|
buf += len % CBPRINTF_PACKAGE_ALIGNMENT;
|
||||||
/*
|
/*
|
||||||
* The space to store the data is represented by both the
|
* The space to store the data is represented by both the
|
||||||
* buffer offset as well as the extra string data to be
|
* buffer offset as well as the extra string data to be
|
||||||
|
@ -324,7 +324,7 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
* Otherwise we must ensure we can store at least
|
* Otherwise we must ensure we can store at least
|
||||||
* the pointer to the format string itself.
|
* the pointer to the format string itself.
|
||||||
*/
|
*/
|
||||||
if ((buf != NULL) && (BUF_OFFSET + sizeof(char *)) > len) {
|
if ((buf0 != NULL) && (BUF_OFFSET + sizeof(char *)) > len) {
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,18 +355,18 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
size = sizeof(int);
|
size = sizeof(int);
|
||||||
|
|
||||||
/* align destination buffer location */
|
/* align destination buffer location */
|
||||||
offset = ROUND_UP(offset, align);
|
buf = (void *)ROUND_UP(buf, align);
|
||||||
|
|
||||||
/* make sure the data fits */
|
/* make sure the data fits */
|
||||||
if (buf != NULL && BUF_OFFSET + size > len) {
|
if (buf0 != NULL && BUF_OFFSET + size > len) {
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
*(int *)(buf + offset) = arg_tag;
|
*(int *)buf = arg_tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += sizeof(int);
|
buf += sizeof(int);
|
||||||
|
|
||||||
if (arg_tag == CBPRINTF_PACKAGE_ARG_TYPE_END) {
|
if (arg_tag == CBPRINTF_PACKAGE_ARG_TYPE_END) {
|
||||||
/* End of arguments */
|
/* End of arguments */
|
||||||
|
@ -430,21 +430,21 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* align destination buffer location */
|
/* align destination buffer location */
|
||||||
offset = ROUND_UP(offset, align);
|
buf = (void *) ROUND_UP(buf, align);
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
/* make sure it fits */
|
/* make sure it fits */
|
||||||
if ((BUF_OFFSET + size) > len) {
|
if ((BUF_OFFSET + size) > len) {
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) {
|
if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) {
|
||||||
memcpy((buf + offset), (uint8_t *)&v, size);
|
memcpy(buf, (uint8_t *)&v, size);
|
||||||
} else if (fmt[-1] == 'L') {
|
} else if (fmt[-1] == 'L') {
|
||||||
*(long double *)(buf + offset) = v.ld;
|
*(long double *)buf = v.ld;
|
||||||
} else {
|
} else {
|
||||||
*(double *)(buf + offset) = v.d;
|
*(double *)buf = v.d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offset += size;
|
buf += size;
|
||||||
parsing = false;
|
parsing = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -577,21 +577,21 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
size = sizeof(double);
|
size = sizeof(double);
|
||||||
}
|
}
|
||||||
/* align destination buffer location */
|
/* align destination buffer location */
|
||||||
offset = ROUND_UP(offset, align);
|
buf = (void *) ROUND_UP(buf, align);
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
/* make sure it fits */
|
/* make sure it fits */
|
||||||
if (BUF_OFFSET + size > len) {
|
if (BUF_OFFSET + size > len) {
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) {
|
if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) {
|
||||||
memcpy(buf + offset, (uint8_t *)&v, size);
|
memcpy(buf, (uint8_t *)&v, size);
|
||||||
} else if (fmt[-1] == 'L') {
|
} else if (fmt[-1] == 'L') {
|
||||||
*(long double *)(buf + offset) = v.ld;
|
*(long double *)buf = v.ld;
|
||||||
} else {
|
} else {
|
||||||
*(double *)(buf + offset) = v.d;
|
*(double *)buf = v.d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offset += size;
|
buf += size;
|
||||||
parsing = false;
|
parsing = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -603,10 +603,10 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* align destination buffer location */
|
/* align destination buffer location */
|
||||||
offset = ROUND_UP(offset, align);
|
buf = (void *) ROUND_UP(buf, align);
|
||||||
|
|
||||||
/* make sure the data fits */
|
/* make sure the data fits */
|
||||||
if ((buf != NULL) && (BUF_OFFSET + size) > len) {
|
if ((buf0 != NULL) && (BUF_OFFSET + size) > len) {
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -614,8 +614,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
|
||||||
if (is_str_arg) {
|
if (is_str_arg) {
|
||||||
s = va_arg(ap, char *);
|
s = va_arg(ap, char *);
|
||||||
process_string:
|
process_string:
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
*(const char **)(buf + offset) = s;
|
*(const char **)buf = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_ro = (fros_cnt-- > 0) ? true : ptr_in_rodata(s);
|
bool is_ro = (fros_cnt-- > 0) ? true : ptr_in_rodata(s);
|
||||||
|
@ -642,7 +642,7 @@ process_string:
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
/*
|
/*
|
||||||
* Remember string pointer location.
|
* Remember string pointer location.
|
||||||
* We will append non-ro strings later.
|
* We will append non-ro strings later.
|
||||||
|
@ -678,34 +678,34 @@ process_string:
|
||||||
|
|
||||||
s_idx++;
|
s_idx++;
|
||||||
}
|
}
|
||||||
offset += sizeof(char *);
|
buf += sizeof(char *);
|
||||||
|
|
||||||
is_str_arg = false;
|
is_str_arg = false;
|
||||||
} else if (size == sizeof(int)) {
|
} else if (size == sizeof(int)) {
|
||||||
int v = va_arg(ap, int);
|
int v = va_arg(ap, int);
|
||||||
|
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
*(int *)(buf + offset) = v;
|
*(int *)buf = v;
|
||||||
}
|
}
|
||||||
offset += sizeof(int);
|
buf += sizeof(int);
|
||||||
} else if (size == sizeof(long)) {
|
} else if (size == sizeof(long)) {
|
||||||
long v = va_arg(ap, long);
|
long v = va_arg(ap, long);
|
||||||
|
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
*(long *)(buf + offset) = v;
|
*(long *)buf = v;
|
||||||
}
|
}
|
||||||
offset += sizeof(long);
|
buf += sizeof(long);
|
||||||
} else if (size == sizeof(long long)) {
|
} else if (size == sizeof(long long)) {
|
||||||
long long v = va_arg(ap, long long);
|
long long v = va_arg(ap, long long);
|
||||||
|
|
||||||
if (buf != NULL) {
|
if (buf0 != NULL) {
|
||||||
if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) {
|
if (Z_CBPRINTF_VA_STACK_LL_DBL_MEMCPY) {
|
||||||
memcpy(buf + offset, (uint8_t *)&v, sizeof(long long));
|
memcpy(buf, (uint8_t *)&v, sizeof(long long));
|
||||||
} else {
|
} else {
|
||||||
*(long long *)(buf + offset) = v;
|
*(long long *)buf = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offset += sizeof(long long);
|
buf += sizeof(long long);
|
||||||
} else {
|
} else {
|
||||||
__ASSERT(false, "unexpected size %u", size);
|
__ASSERT(false, "unexpected size %u", size);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -727,12 +727,12 @@ process_string:
|
||||||
* If all we wanted was to count required buffer size
|
* If all we wanted was to count required buffer size
|
||||||
* then we have it now.
|
* then we have it now.
|
||||||
*/
|
*/
|
||||||
if (buf == NULL) {
|
if (buf0 == NULL) {
|
||||||
return BUF_OFFSET + len - CBPRINTF_PACKAGE_ALIGNMENT;
|
return BUF_OFFSET + len - CBPRINTF_PACKAGE_ALIGNMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clear our buffer header. We made room for it initially. */
|
/* Clear our buffer header. We made room for it initially. */
|
||||||
*(char **)buf = NULL;
|
*(char **)buf0 = NULL;
|
||||||
|
|
||||||
/* Record end of argument list. */
|
/* Record end of argument list. */
|
||||||
pkg_hdr->desc.len = BUF_OFFSET / sizeof(int);
|
pkg_hdr->desc.len = BUF_OFFSET / sizeof(int);
|
||||||
|
@ -767,8 +767,8 @@ process_string:
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
/* store the pointer position prefix */
|
/* store the pointer position prefix */
|
||||||
*(buf + offset) = pos;
|
*buf = pos;
|
||||||
++offset;
|
++buf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -781,13 +781,12 @@ process_string:
|
||||||
|
|
||||||
if (rws_pos_en) {
|
if (rws_pos_en) {
|
||||||
size = 0;
|
size = 0;
|
||||||
*(buf + offset) = str_ptr_arg[i];
|
*buf++ = str_ptr_arg[i];
|
||||||
offset++;
|
|
||||||
} else {
|
} else {
|
||||||
/* retrieve the string pointer */
|
/* retrieve the string pointer */
|
||||||
s = *(char **)(buf + str_ptr_pos[i] * sizeof(int));
|
s = *(char **)(buf0 + str_ptr_pos[i] * sizeof(int));
|
||||||
/* clear the in-buffer pointer (less entropy if compressed) */
|
/* clear the in-buffer pointer (less entropy if compressed) */
|
||||||
*(char **)(buf + str_ptr_pos[i] * sizeof(int)) = NULL;
|
*(char **)(buf0 + str_ptr_pos[i] * sizeof(int)) = NULL;
|
||||||
/* find the string length including terminating '\0' */
|
/* find the string length including terminating '\0' */
|
||||||
size = strlen(s) + 1;
|
size = strlen(s) + 1;
|
||||||
}
|
}
|
||||||
|
@ -797,11 +796,11 @@ process_string:
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
}
|
}
|
||||||
/* store the pointer position prefix */
|
/* store the pointer position prefix */
|
||||||
*(buf + offset) = str_ptr_pos[i];
|
*buf = str_ptr_pos[i];
|
||||||
++offset;
|
++buf;
|
||||||
/* copy the string with its terminating '\0' */
|
/* copy the string with its terminating '\0' */
|
||||||
memcpy(buf + offset, (uint8_t *)s, size);
|
memcpy(buf, (uint8_t *)s, size);
|
||||||
offset += size;
|
buf += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue