This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 2.0][BZ #15670] Replace alloca in __tzfile_read by malloc.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Mike Frysinger <vapier at gentoo dot org>
- Cc: libc-alpha at sourceware dot org, "Joseph S. Myers" <joseph at codesourcery dot com>
- Date: Wed, 16 Oct 2013 18:40:40 +0200
- Subject: [PATCH 2.0][BZ #15670] Replace alloca in __tzfile_read by malloc.
- Authentication-results: sourceware.org; auth=none
- References: <20131014131522 dot GA23174 at domone dot podge> <Pine dot LNX dot 4 dot 64 dot 1310141359220 dot 11037 at digraph dot polyomino dot org dot uk> <20131014144952 dot GA23875 at domone dot podge> <201310141353 dot 21699 dot vapier at gentoo dot org> <20131014182409 dot GA28570 at domone dot podge>
On Mon, Oct 14, 2013 at 08:24:09PM +0200, OndÅej BÃlka wrote:
> > if you're going to switch to malloc, then it seems like using asprintf would
> > be simpler:
> > if (asprintf (&new, "%s/%s", tzdir, file))
> > goto ret_free_transitions;
>
> ti would,
A asprintf simplifies code a bit as a size calculations are now
redundant.
[BZ #15670]
* time/tzfile.c (__tzfile_read): Replace alloca with malloc.
diff --git a/time/tzfile.c b/time/tzfile.c
index 9dd5130..6048d41 100644
--- a/time/tzfile.c
+++ b/time/tzfile.c
@@ -114,6 +114,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
int was_using_tzfile = __use_tzfile;
int trans_width = 4;
size_t tzspec_len;
+ char *new = NULL;
if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
abort ();
@@ -145,22 +146,12 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
if (*file != '/')
{
const char *tzdir;
- unsigned int len, tzdir_len;
- char *new, *tmp;
tzdir = getenv ("TZDIR");
if (tzdir == NULL || *tzdir == '\0')
- {
- tzdir = default_tzdir;
- tzdir_len = sizeof (default_tzdir) - 1;
- }
- else
- tzdir_len = strlen (tzdir);
- len = strlen (file) + 1;
- new = (char *) __alloca (tzdir_len + 1 + len);
- tmp = __mempcpy (new, tzdir, tzdir_len);
- *tmp++ = '/';
- memcpy (tmp, file, len);
+ tzdir = default_tzdir;
+ if (__asprintf (&new, "%s/%s", tzdir, file) == -1)
+ goto ret_free_transitions;
file = new;
}
@@ -173,6 +164,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
{
/* Nothing to do. */
__use_tzfile = 1;
+ free (new);
return;
}
@@ -528,11 +520,13 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
__timezone = -rule_stdoff;
__use_tzfile = 1;
+ free (new);
return;
lose:
fclose (f);
ret_free_transitions:
+ free (new);
free ((void *) transitions);
transitions = NULL;
}