[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH 4/6] libutil: Remove usage of PATH_MAX from has_symlinkloop
From: |
Punit Agrawal |
Subject: |
[RFC PATCH 4/6] libutil: Remove usage of PATH_MAX from has_symlinkloop |
Date: |
Mon, 23 Jan 2017 22:43:37 +0000 |
Update has_symlinkloop to no longer use PATH_MAX. The change relies on
the behaviour of realpath to return an allocated buffer when passed a
NULL second argument.
Ensure that the buffer is freed(not leaked) before returning from the
function.
---
libutil/find.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/libutil/find.c b/libutil/find.c
index a009538..1863df4 100644
--- a/libutil/find.c
+++ b/libutil/find.c
@@ -506,12 +506,12 @@ static int
has_symlinkloop(const char *dir)
{
struct stack_entry *sp;
- char real[PATH_MAX], *p;
- int i;
+ char *real, *p;
+ int i, ret = 0;
if (!strcmp(dir, "./"))
return 0;
- if (realpath(dir, real) == NULL)
+ if ((real = realpath(dir, NULL)) == NULL)
die("cannot get real path of '%s'.", trimpath(dir));
#ifdef SLOOPDEBUG
fprintf(stderr, "======== has_symlinkloop ======\n");
@@ -520,8 +520,10 @@ has_symlinkloop(const char *dir)
fprintf(stderr, "\tcheck '%s' < '%s'\n", real, rootdir);
#endif
p = locatestring(rootdir, real, MATCH_AT_FIRST);
- if (p && (*p == '/' || *p == '\0' || !strcmp(real, "/")))
- return 1;
+ if (p && (*p == '/' || *p == '\0' || !strcmp(real, "/"))) {
+ ret = 1;
+ goto out;
+ }
sp = varray_assign(stack, 0, 0);
#ifdef SLOOPDEBUG
fprintf(stderr, "TEST-2\n");
@@ -530,13 +532,17 @@ has_symlinkloop(const char *dir)
#ifdef SLOOPDEBUG
fprintf(stderr, "%d:\tcheck '%s' == '%s'\n", i, real,
sp[i].real);
#endif
- if (!strcmp(sp[i].real, real))
- return 1;
+ if (!strcmp(sp[i].real, real)) {
+ ret = 1;
+ goto out;
+ }
}
#ifdef SLOOPDEBUG
fprintf(stderr, "===============================\n");
#endif
- return 0;
+out:
+ free(real);
+ return ret;
}
/**
--
2.11.0
- [RFC PATCH 0/6] Enable building Gnu Global package on Hurd, Punit Agrawal, 2017/01/24
- [RFC PATCH 3/6] libutil: Remove usage of PATH_MAX from getrealpath, Punit Agrawal, 2017/01/24
- [RFC PATCH 1/6] libutil: Drop extraneous function declarations, Punit Agrawal, 2017/01/24
- [RFC PATCH 2/6] libutil: Staticize locally used functions, Punit Agrawal, 2017/01/24
- [RFC PATCH 5/6] libutil: Convert rootdir to a dynamically allocated array, Punit Agrawal, 2017/01/24
- [RFC PATCH 4/6] libutil: Remove usage of PATH_MAX from has_symlinkloop,
Punit Agrawal <=
- [RFC PATCH 6/6] libutil: Drop the requirement for PATH_MAX macro, Punit Agrawal, 2017/01/24
- Re: [RFC PATCH 0/6] Enable building Gnu Global package on Hurd, Shigio YAMAGUCHI, 2017/01/25