[klibc] [klibc:master] readlink: Better buffer handling
klibc-bot for H. Peter Anvin
hpa at linux.intel.com
Tue Apr 15 09:36:03 PDT 2014
Commit-ID: 4a66f39cb53fde78c4518615382be83a9e2bff0b
Gitweb: http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=4a66f39cb53fde78c4518615382be83a9e2bff0b
Author: H. Peter Anvin <hpa at linux.intel.com>
AuthorDate: Tue, 15 Apr 2014 09:27:38 -0700
Committer: H. Peter Anvin <hpa at linux.intel.com>
CommitDate: Tue, 15 Apr 2014 09:27:38 -0700
readlink: Better buffer handling
- Allow PATH_MAX bytes, not just a hard-coded 128 bytes
- Just allocate the buffer on the stack, no need for malloc()
- Use puts() rather than printf()
Reported-by: Rafi Rubin <rafi at seas.upenn.edu>
Signed-off-by: H. Peter Anvin <hpa at linux.intel.com>
---
usr/utils/readlink.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/usr/utils/readlink.c b/usr/utils/readlink.c
index 5ea4e41..8b620fd 100644
--- a/usr/utils/readlink.c
+++ b/usr/utils/readlink.c
@@ -13,8 +13,9 @@ static __noreturn usage(void)
int main(int argc, char *argv[])
{
- char *name, *link_name = NULL;
- size_t max_siz = 128;
+ const char *name;
+ char link_name[PATH_MAX];
+ int rv;
progname = *argv++;
@@ -22,15 +23,12 @@ int main(int argc, char *argv[])
if (!name)
usage();
- link_name = malloc(max_siz);
- if (!link_name) {
- perror("malloc");
+ rv = readlink(name, link_name, sizeof link_name - 1);
+ if (rv < 0) {
+ perror(name);
exit(1);
}
-
- if (readlink(name, link_name, max_siz) == -1)
- exit(1);
- printf("%s\n", link_name);
-
+ link_name[rv] = '\0';
+ puts(link_name);
exit(0);
}
More information about the klibc
mailing list