]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
client: fix potential buffer overflow
authorYunchuan Wen <yunchuan.wen@kylin-cloud.com>
Fri, 16 Dec 2016 07:03:17 +0000 (15:03 +0800)
committerYunchuan Wen <yunchuan.wen@kylin-cloud.com>
Fri, 16 Dec 2016 07:03:17 +0000 (15:03 +0800)
Trace::peek_string try to fill char *buf without any length check.
I think string buf is the better way to handle it.

Signed-off-by: Yunchuan Wen <yunchuan.wen@kylin-cloud.com>
src/client/Trace.cc
src/client/Trace.h

index d95b3856e7003b675279954bb8288d72ab6eb32e..43494cd4e7e0c68d0a0705ac8d331d9ff3931cce 100644 (file)
@@ -52,22 +52,22 @@ void Trace::start()
   _line = 1;
 }
 
-const char *Trace::peek_string(char *buf, const char *prefix)
+const char *Trace::peek_string(string &buf, const char *prefix)
 {
   //if (prefix) cout << "prefix '" << prefix << "' line '" << line << "'" << std::endl;
   if (prefix &&
       strstr(line.c_str(), "/prefix") == line.c_str()) {
-    strcpy(buf, prefix);
-    strcpy(buf + strlen(prefix),
-          line.c_str() + strlen("/prefix"));
+    buf.clear();
+    buf.append(prefix);
+    buf.append(line.c_str() + strlen("/prefix"));
   } else {
-    strcpy(buf, line.c_str());
+    buf = line;
   }
-  return buf;
+  return buf.c_str();
 }
 
 
-const char *Trace::get_string(char *buf, const char *prefix)
+const char *Trace::get_string(string &buf, const char *prefix)
 {
   peek_string(buf, prefix);
 
@@ -77,5 +77,5 @@ const char *Trace::get_string(char *buf, const char *prefix)
   getline(*fs, line);
   //cout << "next line is " << line << std::endl;
 
-  return buf;
+  return buf.c_str();
 }
index 7da2eaf896d701184a448e573b65afadaaee3384..2f90c4f7c39736d58b59e27c6666d78c52e49408 100644 (file)
@@ -51,11 +51,11 @@ class Trace {
 
   void start();
 
-  const char *peek_string(char *buf, const char *prefix);
-  const char *get_string(char *buf, const char *prefix);
+  const char *peek_string(string &buf, const char *prefix);
+  const char *get_string(string &buf, const char *prefix);
 
   int64_t get_int() {
-    char buf[20];
+    string buf;
     return atoll(get_string(buf, 0));
   }
   bool end() {