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
tools/nxstyle: Verify relative path in the file header #2982
tools/nxstyle: Verify relative path in the file header #2982
Conversation
eaf6269
to
4f88072
Compare
I wonder if we do not need some way to suppress this check? I often run nxstyle against files outside of the nuttx source tree especially when developing code or testing nxstyle.c. |
tools/nxstyle.c
Outdated
*/ | ||
|
||
const char *root_substr = "/nuttx/"; | ||
char *basedir = strstr(g_file_name, root_substr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this assume that the name of the root directory is nuttx/? That is not always the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is one of the limitations I listed in the description.
I haven't found a nice way of making nxstyle handle both relative and absolute file paths, since the program has no knowledge about the environment.
One way would be to provide the project root path via program arguments, but I think it is a bad solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about this:
const char *tmp = strrstr(g_file_name, line + 2);
if (tmp && strlen(tmp) == strlen(line + 2) &&
(tmp == g_file_name || *(tmp - 1) == '/'))
Yes, it can't catch the well-designed test case, but should enough to catch the most typo error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strrstr
seems to be a C extension specific to Linux. It is not available in macOS.
We could implement it ourselves in nxstyle, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so we can change to:
ssize_t base = strlen(g_file_name) - strlen(line + 2);
if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
strcmp(&g_file_name[base], line + 2) == 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base
seems to assume that the provided relative path in the header has at least has the same amount of characters. But this is usually not the case. If you remove a single char from thetools/nxstyle.c
at the header, it will fail to identify the issue.
Why not? "g_file_name[base - 1] == '/"' should catch it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose the relative file path in the comment is tools/nxstle.c
This condition will not be satisfied because g_file_name[base - 1]
will point to the 't' character.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose the relative file path in the comment is
tools/nxstle.c
If user forget 'y' after 't', strcmp will report the mismatch.
This condition will not be satisfied because
g_file_name[base - 1]
will point to the 't' character.
If user typo 'ools/nxstyle.c', g_file_name[base] equals 'o' and g_file_name[base - 1] equals 't'. Then "g_file_name[base - 1] == '/"' will catch it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, now I get. At first I thought your suggestion's objective was to identify the error, but now I understand that the condition you created will identify the correct file path. So the intended outcome is this:
if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
strcmp(&g_file_name[base], &line[n + 2]) == 0)
{
printf("No error\n");
}
else
{
printf("Error detected\n");
}
Yeah, this seems to work well. But without the knowledge of the project's root path, it will still fail to report an issue when one or more directories are missing in the beginning of the path:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right: my algo can't catch all error, but the frequent error.
A solution for this scenario would be to decouple this check from nxstyle, letting this responsibility to the CI. But the downside is that we would lose the local checking prior to opening the PR. |
We can provide an option to disable the check, so the developer can bypass the check case by case. |
2bdddac
to
6857b81
Compare
6857b81
to
d2ffbee
Compare
d2ffbee
to
cb15be0
Compare
cb15be0
to
8004219
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks @gustavonihei
Summary
Add new verification to nxstyle tool based on the NuttX Coding Standard.
From the 1.1 File Organization section:
Limitations:
For supporting input file provided with absolute path, nxstyle will search for the root folder assuming that it is namednuttx
.When executed directly (e.g. not viacheckpatch.sh
), nxstyle will report a false positive in the following scenarios:Path to the input file starts with./
Path to the input file is not relative to the project root directoryCurrently it does not support the files from the apps/ folder (treated as false negatives).Impact
New verification rule for nxstyle, it will impact CI.
Testing