[PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB
Ahmad Fatoum
a.fatoum at pengutronix.de
Fri Mar 19 18:26:06 GMT 2021
We already return an error code unconditionally when building with
!CONFIG_GLOB. We need to do the same for globfree. Otherwise,
we run risk of corrupting memory.
This issue exists since the code was first added, but it became
more acute with 90cde3b9ff46 ("startup: Execute init scripts in
alphabetical order"), which added a globfree into the shell init.
Configuration without CONFIG_GLOB would from then on experience
memory corruption during startup.
Reported-by: Antony Pavlov <antonynpavlov at gmail.com>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
include/glob.h | 6 +++++-
lib/glob.c | 24 ++++++++++++------------
2 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/include/glob.h b/include/glob.h
index 5f532e6652c3..ec0ac66f8765 100644
--- a/include/glob.h
+++ b/include/glob.h
@@ -177,6 +177,7 @@ extern int glob __P ((__const char *__restrict __pattern, int __flags,
int (*__errfunc) (__const char *, int),
glob_t *__restrict __pglob));
+extern void globfree __P ((glob_t *__pglob));
#else
static inline int glob __P ((__const char *__restrict __pattern, int __flags,
int (*__errfunc) (__const char *, int),
@@ -184,9 +185,12 @@ static inline int glob __P ((__const char *__restrict __pattern, int __flags,
{
return GLOB_ABORTED;
}
+
+static inline void globfree __P ((glob_t *__pglob))
+{
+}
#endif
/* Free storage allocated in PGLOB by a previous `glob' call. */
-extern void globfree __P ((glob_t *__pglob));
#else
extern int glob __P ((__const char *__restrict __pattern, int __flags,
int (*__errfunc) (__const char *, int),
diff --git a/lib/glob.c b/lib/glob.c
index 32f7afdce81b..8523bad9a7ef 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -406,6 +406,18 @@ static int glob_in_dir(const char *pattern, const char *directory,
}
return nfound == 0 ? GLOB_NOMATCH : 0;
}
+
+/* Free storage allocated in PGLOB by a previous `glob' call. */
+void globfree(glob_t *pglob)
+{
+ if (pglob->gl_pathv != NULL) {
+ int i = pglob->gl_flags & GLOB_DOOFFS ? pglob->gl_offs : 0;
+ for (; i < pglob->gl_pathc; ++i)
+ if (pglob->gl_pathv[i] != NULL)
+ free((__ptr_t) pglob->gl_pathv[i]);
+ free((__ptr_t) pglob->gl_pathv);
+ }
+}
#endif /* CONFIG_GLOB */
#ifdef CONFIG_FAKE_GLOB
@@ -443,15 +455,3 @@ glob_t *pglob;
return 0;
}
#endif /* CONFIG_FAKE_GLOB */
-
-/* Free storage allocated in PGLOB by a previous `glob' call. */
-void globfree(glob_t *pglob)
-{
- if (pglob->gl_pathv != NULL) {
- int i = pglob->gl_flags & GLOB_DOOFFS ? pglob->gl_offs : 0;
- for (; i < pglob->gl_pathc; ++i)
- if (pglob->gl_pathv[i] != NULL)
- free((__ptr_t) pglob->gl_pathv[i]);
- free((__ptr_t) pglob->gl_pathv);
- }
-}
--
2.29.2
More information about the barebox
mailing list