* [PATCH] use dynamic buffer for getmntent
@ 2016-09-08 17:07 Natanael Copa
2016-10-20 5:25 ` Rich Felker
0 siblings, 1 reply; 2+ messages in thread
From: Natanael Copa @ 2016-09-08 17:07 UTC (permalink / raw)
To: musl; +Cc: Natanael Copa
overlayfs may have fairly long lines so we use getline to allocate a
buffer dynamically. The buffer will be allocated on first use, expand as
needed, but will never be free'ed.
Downstream bug: http://bugs.alpinelinux.org/issues/5703
Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
---
src/misc/mntent.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/src/misc/mntent.c b/src/misc/mntent.c
index a16d652..722a030 100644
--- a/src/misc/mntent.c
+++ b/src/misc/mntent.c
@@ -3,6 +3,11 @@
#include <mntent.h>
#include <errno.h>
+static char *internal_buf = NULL;
+static size_t internal_bufsize = 0;
+
+#define SENTINEL (char *)&internal_buf
+
FILE *setmntent(const char *name, const char *mode)
{
return fopen(name, mode);
@@ -16,13 +21,17 @@ int endmntent(FILE *f)
struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
{
- int cnt, n[8];
+ int cnt, n[8], use_internal = (linebuf == SENTINEL);
mnt->mnt_freq = 0;
mnt->mnt_passno = 0;
do {
- fgets(linebuf, buflen, f);
+ if (use_internal) {
+ getline(&internal_buf, &internal_bufsize, f);
+ linebuf = internal_buf;
+ } else
+ fgets(linebuf, buflen, f);
if (feof(f) || ferror(f)) return 0;
if (!strchr(linebuf, '\n')) {
fscanf(f, "%*[^\n]%*[\n]");
@@ -49,9 +58,8 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int bufle
struct mntent *getmntent(FILE *f)
{
- static char linebuf[256];
static struct mntent mnt;
- return getmntent_r(f, &mnt, linebuf, sizeof linebuf);
+ return getmntent_r(f, &mnt, SENTINEL, 0);
}
int addmntent(FILE *f, const struct mntent *mnt)
--
2.10.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] use dynamic buffer for getmntent
2016-09-08 17:07 [PATCH] use dynamic buffer for getmntent Natanael Copa
@ 2016-10-20 5:25 ` Rich Felker
0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2016-10-20 5:25 UTC (permalink / raw)
To: musl
On Thu, Sep 08, 2016 at 07:07:31PM +0200, Natanael Copa wrote:
> overlayfs may have fairly long lines so we use getline to allocate a
> buffer dynamically. The buffer will be allocated on first use, expand as
> needed, but will never be free'ed.
>
> Downstream bug: http://bugs.alpinelinux.org/issues/5703
>
> Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
Thanks! Sorry for the delay. Committed with minor style fixes:
> ---
> src/misc/mntent.c | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/src/misc/mntent.c b/src/misc/mntent.c
> index a16d652..722a030 100644
> --- a/src/misc/mntent.c
> +++ b/src/misc/mntent.c
> @@ -3,6 +3,11 @@
> #include <mntent.h>
> #include <errno.h>
>
> +static char *internal_buf = NULL;
> +static size_t internal_bufsize = 0;
Explicit zero initialization is not needed, and current/going-forward
style in musl is not to use the NULL macro but rather 0 or (T*)0 if
needed (e.g. for variadic args).
> +
> +#define SENTINEL (char *)&internal_buf
> +
> FILE *setmntent(const char *name, const char *mode)
> {
> return fopen(name, mode);
> @@ -16,13 +21,17 @@ int endmntent(FILE *f)
>
> struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
> {
> - int cnt, n[8];
> + int cnt, n[8], use_internal = (linebuf == SENTINEL);
>
> mnt->mnt_freq = 0;
> mnt->mnt_passno = 0;
>
> do {
> - fgets(linebuf, buflen, f);
> + if (use_internal) {
> + getline(&internal_buf, &internal_bufsize, f);
> + linebuf = internal_buf;
> + } else
> + fgets(linebuf, buflen, f);
If if clause uses braces, else should too. There might be some
existing violations but I'm trying to make this consistent.
Rich
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-10-20 5:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-08 17:07 [PATCH] use dynamic buffer for getmntent Natanael Copa
2016-10-20 5:25 ` Rich Felker
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).