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

Fix code quality issues in sof tools #8216

Merged
merged 11 commits into from
Sep 26, 2023
Merged

Conversation

softwarecki
Copy link
Collaborator

Fixed many minor bugs in sof tools applications.

Copy link
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @softwarecki , looks good to go!

Copy link
Contributor

@jsarha jsarha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could not find anything else wrong but this commit message appears to be somehow truncated:

logger: convert: Fixed handling of an error reported by clock_gettime
The clock_gettime function only returns information that an error occurred.
The error code should be taken from the e

Signed-off-by: Adrian Warecki [email protected]

@marc-hb
Copy link
Collaborator

marc-hb commented Sep 19, 2023

Please submit this to stable-v2.2 for actual testing

fprintf(out_fd, time_fmt, " TIMESTAMP", "DELTA");
const unsigned int ts_width = timestamp_width(global_config->time_precision);

fprintf(out_fd, "%*s(us)%*s ", -ts_width, " TIMESTAMP", ts_width, "DELTA");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is not equivalent at all. We already discussed this:

This PR has clearly not been tested.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it was done wrong in the linked PR. Run the code below and you will see that both approaches print the same result:

char time_fmt[255];
const unsigned int ts_width = 20;
    
snprintf(time_fmt, sizeof(time_fmt), "%%-%ds(us)%%%ds  ", ts_width, ts_width);
printf(time_fmt, " TIMESTAMP", "DELTA");
printf("\n");
printf("%*s(us)%*s  ", -ts_width, " TIMESTAMP", ts_width, "DELTA");
printf("\n");

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sincere apologies, I had been "burned" by #6738 and this comment was rushed. I didn't know about %*s and it looks great.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, that's what I thought you did it automatically. We discover new things all our lives :)

@marc-hb
Copy link
Collaborator

marc-hb commented Sep 19, 2023

Please submit this to stable-v2.2 for actual testing

BTW smex is not part of the binary release, has never been. It's only part of the build process.

The incoming 2023.9, Intel's sof-bin release will include the sof-logger from... the 2.2 branch. So sof-logger fixes in the main branch will not affect the release.

"Security theater"

smex/elf.c Show resolved Hide resolved
tools/logger/convert.c Show resolved Hide resolved
tools/logger/convert.c Outdated Show resolved Hide resolved
tools/logger/convert.c Show resolved Hide resolved
tools/logger/convert.c Show resolved Hide resolved
tools/logger/logger.c Show resolved Hide resolved
The values assigned when declaring variables were overwritten in the code.
Redundant initialization was removed.

Signed-off-by: Adrian Warecki <[email protected]>
@kv2019i
Copy link
Collaborator

kv2019i commented Sep 19, 2023

@marc-hb @btian1 please re-review. We know this is not part of the firmware code, but improving code quality (both actual like resource leakage on error paths, plus more nice-to-have cases where we have code that is correct but irks pretty much all static code analysis tools) is always welcome.

@marc-hb
Copy link
Collaborator

marc-hb commented Sep 19, 2023

We know this is not part of the firmware code, but improving code quality [...] is always welcome.

Absolutely. However code quality implies running the code changes at least once in at least the default configuration.

(both actual like resource leakage on error paths, plus more nice-to-have cases where we have code that is correct but irks pretty much all static code analysis tools)

I'm totally on board with making code simpler not just for humans but also for analyzers - even when it's already correct. Both usually go together and code "very subtly correct" is likely to be broken later. However there will always be some tricky corner cases and all analyzers offer a process to dispose false positives. Dynamically constructing a format string is likely one of these corner cases.

@kv2019i
Copy link
Collaborator

kv2019i commented Sep 19, 2023

Backport of 7/10 test and submission to stable-v2.2 where we have more coverage in Intel CI for these tools #8231

@marc-hb
Copy link
Collaborator

marc-hb commented Sep 20, 2023

Backport of 7/10 test and submission to stable-v2.2 where we have more coverage in Intel CI for these tools #8231

All 10/10 commits here backported to stable-v2.2 just for testing in #8234

@kv2019i
Copy link
Collaborator

kv2019i commented Sep 20, 2023

Thanks all, the tests with backported 2.2 look good ( #8234 ). So only thing missing is a clean run of the mandatory CI. @softwarecki , can you push a rebase and/or ping CI folks to rerun -- the fail is very unlikely to be related to be caused by this PR, but we still need a clean run.

smex/elf.c Outdated
ret = fread(*dst_buff, 1, section->size, module->fd);
if (ret != section->size) {
fprintf(stderr, "error: can't read %s section %d\n", section_name, -errno);
ret = -ENODATA;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do something like ret = ret < 0 ? -errno : -ENODATA; but at least this doesn't miss errors

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. I fixed returned error codes in another places.

Added checking of the value returned by fseek function and added memory
release when an error is detected.

Signed-off-by: Adrian Warecki <[email protected]>
If fseek reports an error, the error code set in errno is returned.
Improved handling an error during reading from a file. Now it distinguishes
between an error reported by the fread and insufficient data in a file.

Signed-off-by: Adrian Warecki <[email protected]>
String terminator was added to the buffer with a list of section names
in the elf file. Added check to the section name index to make sure it
doesn't go beyond the buffer size.

Signed-off-by: Adrian Warecki <[email protected]>
The clock_gettime function only returns information that an error occurred.
The error code should be taken from the errno variable.

Signed-off-by: Adrian Warecki <[email protected]>
@lgirdwood
Copy link
Member

@softwarecki can you check CI results. Thanks

Added checking of value returned by file operation functions. In case of an
error, message is printed and error code is returned.

Signed-off-by: Adrian Warecki <[email protected]>
The timestamp printing process has been simplified by eliminating the
dynamic creation of the formatting string. All necessary parameters are now
passed directly to the printing function.

Signed-off-by: Adrian Warecki <[email protected]>
…inated

Added a null string terminator to be sure that strings read from a file are
terminated correctly.

Signed-off-by: Adrian Warecki <[email protected]>
The precision check condition has been simplified, the unsigned value
cannot be negative. Added definitions containing an error message instead
of using a constant variable.

Signed-off-by: Adrian Warecki <[email protected]>
Used string manipulation functions that check the size of the available
buffer.

Signed-off-by: Adrian Warecki <[email protected]>
@softwarecki
Copy link
Collaborator Author

@softwarecki can you check CI results. Thanks

I fixed one checkpatch issue.

Its look like we have some problem with CI for windows platform...

@lgirdwood
Copy link
Member

@softwarecki can you check CI results. Thanks

I fixed one checkpatch issue.

Its look like we have some problem with CI for windows platform...

Yep, I'm surprised I'm not hearing any Windows developer complain here unless the Windows build config locally has Ninja and is out of sync with the container ?

@marc-hb
Copy link
Collaborator

marc-hb commented Sep 22, 2023

Yep, I'm surprised I'm not hearing any Windows developer complain here unless the Windows build config locally has Ninja and is out of sync with the container ?

It looks like an intermittent Github glitch, see above. Try re-running that one.

@marc-hb
Copy link
Collaborator

marc-hb commented Sep 22, 2023

Its look like we have some problem with CI for windows platform...

Filed new

@kv2019i
Copy link
Collaborator

kv2019i commented Sep 25, 2023

@softwarecki We need mandatory CIs to pass, this may need a repush (or manually trigger at quickbuild).

Improved release of resources when an error is detected.

Signed-off-by: Adrian Warecki <[email protected]>
@softwarecki
Copy link
Collaborator Author

@kv2019i CI is all green now :) (except checkpatch which complains about camel case in Elf32_Shdr)

@lgirdwood
Copy link
Member

@softwarecki We need mandatory CIs to pass, this may need a repush (or manually trigger at quickbuild).

@wszypelt

@kv2019i CI is all green now :) (except checkpatch which complains about camel case in Elf32_Shdr)

@softwarecki can you check internal CI. I think we are good once it's green.

@lgirdwood lgirdwood merged commit 966ad48 into thesofproject:main Sep 26, 2023
40 of 41 checks passed
@softwarecki softwarecki deleted the backport branch October 6, 2023 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants