From 504a5ced05f48bf8cc1a08b22ce8830b6db98d41 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 1 Jun 2016 22:32:37 +0000 Subject: http_put: gracefully handle path allocation errors Failing to allocate memory should be a temporary error and be non-fatal. --- http_put.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/http_put.c b/http_put.c index 92bc435..5084090 100644 --- a/http_put.c +++ b/http_put.c @@ -306,6 +306,8 @@ MOG_NOINLINE static void rnd_init_per_thread(void) static char *tmppath_for(struct mog_http *http, const char *path) { int32_t result; + int rc; + char *s; if (!rnd.ready) rnd_init_per_thread(); @@ -313,8 +315,10 @@ static char *tmppath_for(struct mog_http *http, const char *path) assert(http && "validation later"); /* TODO */ CHECK(int, 0, random_r(&rnd.data, &result)); - return xasprintf("%s.%08x.%d.tmp", - path, (unsigned)result, (int)getpid()); + rc = asprintf(&s, "%s.%08x.%d.tmp", + path, (unsigned)result, (int)getpid()); + + return rc >= 0 ? s : 0; } static struct mog_file * open_put(struct mog_http *http, char *path) @@ -337,12 +341,17 @@ static struct mog_file * open_put(struct mog_http *http, char *path) char *tmp = tmppath_for(http, path); int fl = O_EXCL | O_TRUNC | O_CREAT; + if (!tmp) + return NULL; + http->forward = mog_file_open_put(http->svc, tmp, fl); /* retry once on EEXIST, don't inf loop if RNG is broken */ if (http->forward == NULL && errno == EEXIST) { free(tmp); tmp = tmppath_for(http, path); + if (!tmp) + return NULL; http->forward = mog_file_open_put(http->svc, tmp, fl); } if (http->forward == NULL) { -- cgit v1.2.3-24-ge0c7