cmd: use safer functions in sc_mount_opt2str #2778

Merged
merged 3 commits into from Feb 7, 2017

Conversation

Projects
None yet
3 participants
Contributor

zyga commented Feb 3, 2017

This branch changes sc_mount_opt2str to use sc_string_append, sc_must_snprintf and strnlen
instead of the traditional and unchecked equivalents from the standard library.

This change now requires the caller to provide a buffer so all the code using this function
was patched to adapt. The buffer size stayed the same as the old hard-coded internal buffer.

Signed-off-by: Zygmunt Krynicki zygmunt.krynicki@canonical.com

zyga added some commits Feb 3, 2017

cmd: use external buffers in sc_mount_opt2str
This patch changes the aforementioned function to take an external buffer and
use it safely with sc_string_append instead of strcat.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
cmd: use sc_must_snprintf instead of sprintf
Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
cmd: use strnlen instead of strlen
Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>

mvo5 approved these changes Feb 7, 2017

Looks fine, one nitpick.

@@ -32,6 +32,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "cannot parse given argument as a number\n");
return 1;
}
- printf("%#lx is %s\n", mountflags, sc_mount_opt2str(mountflags));
+ char buf[1000];
+ printf("%#lx is %s\n", mountflags, sc_mount_opt2str(buf, sizeof buf, mountflags));
@mvo5

mvo5 Feb 7, 2017

Collaborator

(nitpick) I noticed we have this a lot in the code but I personally prefer the parenthese around sizeof(buf). I know its not strictly needed but sometimes it and sometimes it is not and thats why I prefer consistency (but not a blocker if everyone else is fine with this).

@zyga

zyga Feb 7, 2017

Contributor

Ha, this is a weird part of C syntax: sizeof expr does not need parentheses but sizeof(type) does.

@jdstrand

jdstrand Feb 7, 2017

Contributor

FWIW, I prefer consistency here too.

@zyga zyga requested a review from jdstrand Feb 7, 2017

Thanks for this PR-- liking the cleanups here. LGTM with the one small future-proofing comment (and again, +1 on 'sizeof(...)' for consistency).

@@ -32,6 +32,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "cannot parse given argument as a number\n");
return 1;
}
- printf("%#lx is %s\n", mountflags, sc_mount_opt2str(mountflags));
+ char buf[1000];
+ printf("%#lx is %s\n", mountflags, sc_mount_opt2str(buf, sizeof buf, mountflags));
@mvo5

mvo5 Feb 7, 2017

Collaborator

(nitpick) I noticed we have this a lot in the code but I personally prefer the parenthese around sizeof(buf). I know its not strictly needed but sometimes it and sometimes it is not and thats why I prefer consistency (but not a blocker if everyone else is fine with this).

@zyga

zyga Feb 7, 2017

Contributor

Ha, this is a weird part of C syntax: sizeof expr does not need parentheses but sizeof(type) does.

@jdstrand

jdstrand Feb 7, 2017

Contributor

FWIW, I prefer consistency here too.

}
// Chop the excess comma from the end.
- size_t len = strlen(buf);
+ size_t len = strnlen(buf, buf_size);
if (len > 0 && buf[len - 1] == ',') {
@jdstrand

jdstrand Feb 7, 2017

Contributor

strnlen() can return buf_size here which would indicate buf is a non-terminated string. Now, because of all the sc_string_append() usage, this shouldn't happen, but to future-proof the code I suggest adding:

if (len == buf_size) {
    die(...);
} else if (len > 0 && buf[len - 1] == ',') {
    ...
@zyga

zyga Feb 7, 2017

Contributor

Thanks for the tip. I don't have the instinct developed for using strnlen (imagine I worked me entire life without knowing it exited). I'll definitely be on the lookout for this more.

@zyga zyga merged commit 9cf8d9d into snapcore:master Feb 7, 2017

6 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-amd64 autopkgtest finished (success)
Details
xenial-i386 autopkgtest finished (success)
Details
xenial-ppc64el autopkgtest finished (success)
Details
yakkety-amd64 autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details

@zyga zyga deleted the zyga:safer-mount-opt2str branch Feb 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment