Rafi Rubin
2014-Apr-15 00:33 UTC
[klibc] [PATCH] Null terminate before printing the link name.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 This fixes garbage I'm seeing consistently from readlink /dev/disk/by-uuid/..... during boot. Signed-off-by: Rafi Rubin <rafi at seas.upenn.edu> - --- usr/utils/readlink.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/usr/utils/readlink.c b/usr/utils/readlink.c index 5ea4e41..75a0735 100644 - --- a/usr/utils/readlink.c +++ b/usr/utils/readlink.c @@ -15,6 +15,7 @@ int main(int argc, char *argv[]) { char *name, *link_name = NULL; size_t max_siz = 128; + size_t actual_siz = 0; progname = *argv++; @@ -28,8 +29,9 @@ int main(int argc, char *argv[]) exit(1); } - - if (readlink(name, link_name, max_siz) == -1) + if ((actual_siz = readlink(name, link_name, max_siz)) == -1) exit(1); + link_name[actual_siz] = '\0'; printf("%s\n", link_name); exit(0); - -- 1.9.1 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 Comment: Using GnuPG with Icedove - http://www.enigmail.net/ iQIcBAEBAgAGBQJTTH5VAAoJEPILXytRLnK2nfYP/3w7c0dyPC2sQVvXK4gckgo6 UclX5GG9ALfH7GtFRHAzfYLrSi9O5Jcub1711UY0NItKA2mCW5sU8joHD5QWRqxn +4J2b3Bd8BfUuMM/+pjenVlhGYQJWs447cTR4TW8nAxQ1/86I1k4cjGpuugQhp2W 5MwVY/SLagTdydIYrEvrWhAftujiBPN4wAvB2wB8SWrPyLAr9J8H4rSgOTplb2vt TzvtpCulg5Xa4TuOWSkBpyKjv2ThgizDwgzCinJdkqfpPGz8DoqlaYrfg4mSrqhT JFA9flNKqF0LlP/cCOc2E+9K1W+HZfodmqyGAabDXmXd4/s89wyosPfpj0E4Cx5G AhJbMXL0qukZIjgJd6Q4MJNWbW2PefjDHQZbX6jHtMdTXekiiN7+1zs4bZAbjJOO bywOUlEdZiqcOkAEIOMjF5pK7JRnUo40Y0f21mVJFP/u/9Bc/r2dHUn/8Bm8KF/H BaY0CGxTdu8Bl2bcsbxB76XksroGnj/a+lax0dpqWMhLlQCrUiYtgkXwPex/Lkc0 kZwMvaUPo4qNGCLNRNJOOXhPQ/nlPspzmjgqPm4Sq9xniMCqqyKSAxwQiu386vRc Byijbw3PGUtpdcUu0ouSjYeraTML03BRXzNBZ4jlLn+ADoGVrPOJy1AIMxzBFdY2 eBOBa3At3b8A1AIOMOQY =KbjI -----END PGP SIGNATURE-----
Neal Murphy
2014-Apr-15 01:05 UTC
[klibc] [PATCH] Null terminate before printing the link name.
You also want: link_name = malloc(max_siz + 1); (This is from v2.0.1). Otherwise you'd write past the end of the buffer when the link happens to be 128 bytes long. Granted, it's not critical in *this* application, but it's good practice. N On Monday, April 14, 2014 08:33:29 PM Rafi Rubin wrote:> This fixes garbage I'm seeing consistently from readlink > /dev/disk/by-uuid/..... during boot. > > Signed-off-by: Rafi Rubin <rafi at seas.upenn.edu> > --- > usr/utils/readlink.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/usr/utils/readlink.c b/usr/utils/readlink.c > index 5ea4e41..75a0735 100644 > --- a/usr/utils/readlink.c > +++ b/usr/utils/readlink.c > @@ -15,6 +15,7 @@ int main(int argc, char *argv[]) > { > char *name, *link_name = NULL; > size_t max_siz = 128; > + size_t actual_siz = 0; > > progname = *argv++; > > @@ -28,8 +29,9 @@ int main(int argc, char *argv[]) > exit(1); > } > > - if (readlink(name, link_name, max_siz) == -1) > + if ((actual_siz = readlink(name, link_name, max_siz)) == -1) > exit(1); > + link_name[actual_siz] = '\0'; > printf("%s\n", link_name); > > exit(0);-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.zytor.com/pipermail/klibc/attachments/20140414/ea14f2f1/attachment.html>