Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: int-uint comparsion causes false assert & epic hang #40115

Closed
emillindq opened this issue Nov 5, 2021 · 1 comment · Fixed by #40253
Closed

logging: int-uint comparsion causes false assert & epic hang #40115

emillindq opened this issue Nov 5, 2021 · 1 comment · Fixed by #40253
Assignees
Labels
area: Logging bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@emillindq
Copy link
Contributor

Describe the bug
This piece of code

uint32_t log_filter_get(struct log_backend const *const backend,
uint32_t domain_id, int16_t source_id, bool runtime)
{
__ASSERT_NO_MSG(source_id < z_log_sources_count());
if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && runtime) {
if (source_id < 0) {
return LOG_LEVEL_DBG;
}
uint32_t *filters = z_log_dynamic_filters_get(source_id);
return LOG_FILTER_SLOT_GET(filters,
log_backend_id_get(backend));
}
return log_compiled_level_get(source_id);
}

source_id is int16_t and can take the value -1 according to the if statement. Now, z_log_sources_count() returns uint32_t which causes a classical int-uint comparison causing the -1 to become UINT32_MAX when converted. This is what happened for me anyways.
This in turn will trigger an assert, which in the assert will also be -1, which somehow causes the debugger to loose connection with target.

I want to make a PR for this, but my question for this is how to solve this in the best way? Typecase the z_log_sources_count() to int before comparing?

@emillindq emillindq added the bug The issue is a bug, or the PR is fixing a bug label Nov 5, 2021
@emillindq
Copy link
Contributor Author

Obviously this only shows itself if building with ASSERTS active and some special cases, for example (I think) when redirecting PRINTK to LOG subsystem in which I think source_id is -1

@cfriedt cfriedt added priority: low Low impact/importance bug area: Logging labels Nov 9, 2021
emillindq added a commit to emillindq/zephyr that referenced this issue Nov 10, 2021
If source_id is -1, which is a valid value, it will be
converted to unsigned since it's compared with an unsigned
which means it will be huge and asserts will trigger. To avoid
this, we typecast the unsigned part to signed.

Resolves zephyrproject-rtos#40115

Signed-off-by: Emil Lindqvist <emil@lindq.gr>
nashif pushed a commit that referenced this issue Nov 15, 2021
If source_id is -1, which is a valid value, it will be
converted to unsigned since it's compared with an unsigned
which means it will be huge and asserts will trigger. To avoid
this, we typecast the unsigned part to signed.

Resolves #40115

Signed-off-by: Emil Lindqvist <emil@lindq.gr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Logging bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants