summaryrefslogtreecommitdiffstats
path: root/libc/tzcode
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2014-04-25 16:55:04 -0700
committerElliott Hughes <enh@google.com>2014-04-25 21:51:26 -0700
commit329103d3e2f42d3e35a060c3e0d326d6476eb2ef (patch)
tree0624fa58c29f43ffe405be7fa47a73f85ad29fae /libc/tzcode
parentf2dc7d17196de9904df67f21aad1525b8568a705 (diff)
downloadbionic-329103d3e2f42d3e35a060c3e0d326d6476eb2ef.zip
bionic-329103d3e2f42d3e35a060c3e0d326d6476eb2ef.tar.gz
bionic-329103d3e2f42d3e35a060c3e0d326d6476eb2ef.tar.bz2
Don't use so much stack in tzcode.
Bug: 14313703 Bug: https://code.google.com/p/android/issues/detail?id=61130 Change-Id: Id9b240fce20d4a6f2660792070e02b2b5f287655
Diffstat (limited to 'libc/tzcode')
-rw-r--r--libc/tzcode/localtime.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/libc/tzcode/localtime.c b/libc/tzcode/localtime.c
index 6e7eaa9..3efa5dd 100644
--- a/libc/tzcode/localtime.c
+++ b/libc/tzcode/localtime.c
@@ -2115,11 +2115,17 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
return -1;
}
- char path[PATH_MAX];
- snprintf(path, sizeof(path), "%s/%s", path_prefix, path_suffix);
+ size_t path_length = strlen(path_prefix) + 1 + strlen(path_suffix) + 1;
+ char* path = malloc(path_length);
+ if (path == NULL) {
+ fprintf(stderr, "%s: couldn't allocate %zu-byte path\n", __FUNCTION__, path_length);
+ return -1;
+ }
+ snprintf(path, path_length, "%s/%s", path_prefix, path_suffix);
int fd = TEMP_FAILURE_RETRY(open(path, OPEN_MODE));
if (fd == -1) {
XLOG(("%s: could not open \"%s\": %s\n", __FUNCTION__, path, strerror(errno)));
+ free(path);
return -2; // Distinguish failure to find any data from failure to find a specific id.
}
@@ -2138,6 +2144,7 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
if (bytes_read != sizeof(header)) {
fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
__FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
+ free(path);
close(fd);
return -1;
}
@@ -2145,6 +2152,7 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n",
__FUNCTION__, path, header.tzdata_version);
+ free(path);
close(fd);
return -1;
}
@@ -2159,6 +2167,7 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n",
__FUNCTION__, path, strerror(errno));
+ free(path);
close(fd);
return -1;
}
@@ -2166,9 +2175,17 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
off_t specific_zone_offset = -1;
ssize_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset);
char* index = malloc(index_size);
+ if (index == NULL) {
+ fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n",
+ __FUNCTION__, index_size, path);
+ free(path);
+ close(fd);
+ return -1;
+ }
if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != index_size) {
fprintf(stderr, "%s: could not read index of \"%s\": %s\n",
__FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
+ free(path);
free(index);
close(fd);
return -1;
@@ -2201,6 +2218,7 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
if (specific_zone_offset == -1) {
XLOG(("%s: couldn't find zone \"%s\"\n", __FUNCTION__, olson_id));
+ free(path);
close(fd);
return -1;
}
@@ -2208,12 +2226,14 @@ static int __bionic_open_tzdata_path(const char* path_prefix_variable, const cha
if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
__FUNCTION__, specific_zone_offset, path, strerror(errno));
+ free(path);
close(fd);
return -1;
}
// TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not.
+ free(path);
return fd;
}