From: Yehuda Sadeh Date: Sun, 20 May 2012 22:07:57 +0000 (-0700) Subject: check malloc return values X-Git-Tag: v0.48argonaut~137^2~16^2^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=7eb29aeec6f75f482d9ea3b2f94eb6899b830c60;p=ceph.git check malloc return values There were a few places where we didn't check malloc return code. Signed-off-by: Yehuda Sadeh --- diff --git a/src/common/BackTrace.cc b/src/common/BackTrace.cc index aec5cb2be462..ebbb33c01451 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 d08f447755a5..e339205b5ea5 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 87d9a829befc..0f4e322b27a7 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 b990189a1cf3..4c96ed77c784 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 75b413d60eda..1b21d2f5171e 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 63df9e7c4ccb..49be99284ac9 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 38f1ca9527a9..c8c0f86a0087 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 b0df632b20c9..c3bdb38d158b 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 0b0527a19b88..aa00581a0e18 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();