]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
check malloc return values
authorYehuda Sadeh <yehuda@hq.newdream.net>
Sun, 20 May 2012 22:07:57 +0000 (15:07 -0700)
committerSage Weil <sage@inktank.com>
Sun, 20 May 2012 22:12:33 +0000 (15:12 -0700)
There were a few places where we didn't check malloc return code.

Signed-off-by: Yehuda Sadeh <yehuda@hq.newdream.net>
src/common/BackTrace.cc
src/common/ConfUtils.cc
src/common/Thread.cc
src/common/buffer.cc
src/common/ceph_argparse.cc
src/common/config.cc
src/include/addr_parsing.c
src/mount/canonicalize.c
src/objclass/class_api.cc

index aec5cb2be4625b2489405ef7a188ac350d5f46e1..ebbb33c014510afd87c7f0575b7f6575ab0c87c2 100644 (file)
@@ -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;
 
index d08f447755a581468b152db6e572af4f170747a7..e339205b5ea58b69dc3da6c52bb7bc184a8d26d7 100644 (file)
@@ -126,6 +126,10 @@ parse_file(const std::string &fname, std::deque<std::string> *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)) {
index 87d9a829befc5fd8e93949f278ce81a10112b15a..0f4e322b27a71870d9c85dc8cf201edf7932b044 100644 (file)
@@ -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);
   }
index b990189a1cf33e0a0770f4c23c59683afada792b..4c96ed77c784a5bda2c1b6cc2b8712cd9c8de5bc 100644 (file)
@@ -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;
     }
index 75b413d60edaa27c701c4484b4bc9c15543b1847..1b21d2f5171e4f9c2c4de3bee88ea67116c403cc 100644 (file)
@@ -85,6 +85,8 @@ void vec_to_argv(std::vector<const char*>& args,
   if (argc && argv)
     myname = argv[0];
   argv = (const char**)malloc(sizeof(char*) * argc);
+  if (!argv)
+    throw bad_alloc();
   argc = 1;
   argv[0] = myname;
 
index 63df9e7c4ccb6005423fd80308b3c4e6f8581df4..49be99284ac9cf4d3c5bd4b4f2891d20db8b7f7b 100644 (file)
@@ -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;
     }
index 38f1ca9527a9aecd644c383f1431c97f515fd4ba..c8c0f86a0087e2ee3bcf08af4b0755ffed4dbca7 100644 (file)
@@ -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;
 
index b0df632b20c983e77a4c7778c93ffcff5d249866..c3bdb38d158bbf33870018f6724a80b7d36cf4b0 100644 (file)
@@ -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);
index 0b0527a19b88d838936b8af0152bc933eae8e8f7..aa00581a0e189d8e43952c61a663b6df904e6501 100644 (file)
@@ -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();