From 7eb29aeec6f75f482d9ea3b2f94eb6899b830c60 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Sun, 20 May 2012 15:07:57 -0700 Subject: [PATCH] check malloc return values There were a few places where we didn't check malloc return code. Signed-off-by: Yehuda Sadeh --- src/common/BackTrace.cc | 4 ++++ src/common/ConfUtils.cc | 4 ++++ src/common/Thread.cc | 2 ++ src/common/buffer.cc | 7 +++++-- src/common/ceph_argparse.cc | 2 ++ src/common/config.cc | 2 ++ src/include/addr_parsing.c | 2 ++ src/mount/canonicalize.c | 4 ++++ src/objclass/class_api.cc | 6 ++++++ 9 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/common/BackTrace.cc b/src/common/BackTrace.cc index aec5cb2be4625..ebbb33c014510 100644 --- a/src/common/BackTrace.cc +++ b/src/common/BackTrace.cc @@ -22,6 +22,8 @@ void BackTrace::print(std::ostream& out) size_t sz = 1024; // just a guess, template names will go much wider char *function = (char *)malloc(sz); + if (!function) + return; char *begin = 0, *end = 0; // find the parentheses and address offset surrounding the mangled name @@ -34,6 +36,8 @@ void BackTrace::print(std::ostream& out) if (begin && end) { int len = end - begin; char *foo = (char *)malloc(len+1); + if (!foo) + return; memcpy(foo, begin, len); foo[len] = 0; diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index d08f447755a58..e339205b5ea58 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -126,6 +126,10 @@ parse_file(const std::string &fname, std::deque *errors) sz = (size_t)st_buf.st_size; buf = (char*)malloc(sz); + if (!buf) { + ret = -ENOMEM; + goto done; + } if (fread(buf, 1, sz, fp) != sz) { if (ferror(fp)) { diff --git a/src/common/Thread.cc b/src/common/Thread.cc index 87d9a829befc5..0f4e322b27a71 100644 --- a/src/common/Thread.cc +++ b/src/common/Thread.cc @@ -71,6 +71,8 @@ int Thread::try_create(size_t stacksize) stacksize &= CEPH_PAGE_MASK; // must be multiple of page if (stacksize) { thread_attr = (pthread_attr_t*) malloc(sizeof(pthread_attr_t)); + if (!thread_attr) + return -ENOMEM; pthread_attr_init(thread_attr); pthread_attr_setstacksize(thread_attr, stacksize); } diff --git a/src/common/buffer.cc b/src/common/buffer.cc index b990189a1cf33..4c96ed77c784a 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -88,10 +88,13 @@ bool buffer_track_alloc = get_env_bool("CEPH_BUFFER_TRACK"); class buffer::raw_malloc : public buffer::raw { public: raw_malloc(unsigned l) : raw(l) { - if (len) + if (len) { data = (char *)malloc(len); - else + if (!data) + throw bad_alloc(); + } else { data = 0; + } inc_total_alloc(len); bdout << "raw_malloc " << this << " alloc " << (void *)data << " " << l << " " << buffer::get_total_alloc() << bendl; } diff --git a/src/common/ceph_argparse.cc b/src/common/ceph_argparse.cc index 75b413d60edaa..1b21d2f5171e4 100644 --- a/src/common/ceph_argparse.cc +++ b/src/common/ceph_argparse.cc @@ -85,6 +85,8 @@ void vec_to_argv(std::vector& args, if (argc && argv) myname = argv[0]; argv = (const char**)malloc(sizeof(char*) * argc); + if (!argv) + throw bad_alloc(); argc = 1; argv[0] = myname; diff --git a/src/common/config.cc b/src/common/config.cc index 63df9e7c4ccb6..49be99284ac9c 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -695,6 +695,8 @@ int md_config_t::_get_val(const char *key, char **buf, int len) const int l = strlen(str.c_str()) + 1; if (len == -1) { *buf = (char*)malloc(l); + if (!*buf) + return -ENOMEM; strcpy(*buf, str.c_str()); return 0; } diff --git a/src/include/addr_parsing.c b/src/include/addr_parsing.c index 38f1ca9527a9a..c8c0f86a0087e 100644 --- a/src/include/addr_parsing.c +++ b/src/include/addr_parsing.c @@ -55,6 +55,8 @@ char *resolve_addrs(const char *orig_str) len = BUF_SIZE; new_str = (char *)malloc(len); + if (!new_str) + return NULL; pos = 0; diff --git a/src/mount/canonicalize.c b/src/mount/canonicalize.c index b0df632b20c98..c3bdb38d158bb 100644 --- a/src/mount/canonicalize.c +++ b/src/mount/canonicalize.c @@ -57,6 +57,8 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) { /* Expand each slash-separated pathname component. */ link_path = malloc(PATH_MAX+1); + if (!link_path) + return NULL; while (*path != '\0') { /* Ignore stray "/" */ if (*path == '/') { @@ -179,6 +181,8 @@ canonicalize_path(const char *path) return NULL; canonical = malloc(PATH_MAX+2); + if (!canonical) + return NULL; if (!myrealpath(path, canonical, PATH_MAX+1)) { free(canonical); return strdup(path); diff --git a/src/objclass/class_api.cc b/src/objclass/class_api.cc index 0b0527a19b88d..aa00581a0e189 100644 --- a/src/objclass/class_api.cc +++ b/src/objclass/class_api.cc @@ -92,6 +92,8 @@ int cls_call(cls_method_context_t hctx, const char *cls, const char *method, r = (*pctx)->pg->do_osd_ops(*pctx, nops); *outdata = (char *)malloc(op.outdata.length()); + if (!*outdata) + return -ENOMEM; memcpy(*outdata, op.outdata.c_str(), op.outdata.length()); *outdatalen = op.outdata.length(); @@ -113,6 +115,8 @@ int cls_getxattr(cls_method_context_t hctx, const char *name, r = (*pctx)->pg->do_osd_ops(*pctx, nops); *outdata = (char *)malloc(op.outdata.length()); + if (!*outdata) + return -ENOMEM; memcpy(*outdata, op.outdata.c_str(), op.outdata.length()); *outdatalen = op.outdata.length(); @@ -149,6 +153,8 @@ int cls_read(cls_method_context_t hctx, int ofs, int len, int r = (*pctx)->pg->do_osd_ops(*pctx, ops); *outdata = (char *)malloc(ops[0].outdata.length()); + if (!*outdata) + return -ENOMEM; memcpy(*outdata, ops[0].outdata.c_str(), ops[0].outdata.length()); *outdatalen = ops[0].outdata.length(); -- 2.39.5