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

shell/vfs: Handle print failure in genfile cmd gracefully #20595

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

Teufelchen1
Copy link
Contributor

Contribution description

Hey 🐡

snprintf turned out to be difficult to get right.

Testing procedure

Try make -C tests/sys/vfs_default/ all-asan term and play with `genfile

Issues/PRs references

@github-actions github-actions bot added the Area: sys Area: System label Apr 18, 2024
@Teufelchen1 Teufelchen1 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 18, 2024
@riot-ci
Copy link

riot-ci commented Apr 18, 2024

Murdock results

✔️ PASSED

8bd1de8 shell/vfs: Handle print failure in genfile cmd gracefully

Success Failures Total Runtime
10065 0 10066 18m:59s

Artifacts

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

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

Thanks! This means we just silently ignore the truncation if it happens?

sys/shell/cmds/vfs.c Outdated Show resolved Hide resolved
@@ -744,7 +744,19 @@ static void _write_block(int fd, unsigned bs, unsigned i)
char block[bs];
char *buf = block;

buf += snprintf(buf, bs, "|%03u|", i);
int size_wanted = snprintf(buf, bs, "|%03u|", i);
Copy link
Contributor

@kfessel kfessel Apr 19, 2024

Choose a reason for hiding this comment

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

the return value of this is 5 %03u is constant width unless it failed -> bs needs to be 6 to have blocks big enough ( fit this print and a /n )

this would be more readable (to me) if instead of pointer arithmetics, adresses of array positions would be used (in stead of *buf have unsigned pos)

*buf + size

vs

&blocks[pos]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm not entirely sure but while thinking of it, I found a neat little optimization. I moved the memset into the if-clause, please have a look.

@Teufelchen1
Copy link
Contributor Author

This means we just silently ignore the truncation if it happens

Yes, I am not sure what would be best, so I thought: Not writing to a fs is better than writing truncated garbage to the fs.

Copy link
Contributor

@kfessel kfessel left a comment

Choose a reason for hiding this comment

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

handles snprintf return value better than before avoiding mishaps

Copy link
Contributor

@kfessel kfessel left a comment

Choose a reason for hiding this comment

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

expected results:

(any, 3, 8) will write "|0\n"
(any, 5, 8) will write "|008\n"
(any, 6, 8) will write "|008|\n"
(any, 9, 8) will write "|008|\0\0\0\n"

(any, 3, 8000) will write "|8\n"
(any, 5, 8000) will write "|800\n"
(any, 6, 8000) will write "|8000\n"
(any, 9, 8000) will write "|8000|\0\0\n"

@Teufelchen1 Teufelchen1 added this pull request to the merge queue Apr 25, 2024
Merged via the queue into RIOT-OS:master with commit 5409364 Apr 25, 2024
26 checks passed
@mguetschow mguetschow added this to the Release 2024.07 milestone Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants