Browse Source

made deocde_blob() exception safe.
additional cleanups:
- unify decode_blob() and decode_utf() since these two are identical.
moved the XXX comment to the caller side.
- costified the length argument to decode_blob(); it doesn't have to be
adjusted.
- removed an unnecessary temporary variable (it didn't seem to improve
readability)


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@1664 e5f2f494-b856-4b98-b285-d166d9295462

JINMEI Tatuya 15 years ago
parent
commit
231bbc395f
1 changed files with 6 additions and 29 deletions
  1. 6 29
      src/lib/cc/data.cc

+ 6 - 29
src/lib/cc/data.cc

@@ -703,40 +703,16 @@ decode_real(std::stringstream& in, int& item_length UNUSED_PARAM) {
 }
 }
 
 
 ElementPtr
 ElementPtr
-decode_blob(std::stringstream& in, int& item_length) {
-    char *buf = new char[item_length + 1];
+decode_blob(std::stringstream& in, const int item_length) {
+    vector<char> buf(item_length + 1);
 
 
-    in.read(buf, item_length);
+    in.read(&buf[0], item_length);
     if (in.fail()) {
     if (in.fail()) {
-        delete[] buf;
         throw DecodeError();
         throw DecodeError();
     }
     }
     buf[item_length] = 0;
     buf[item_length] = 0;
 
 
-    std::string s = std::string(buf, item_length);
-    item_length -= item_length;
-
-    delete [] buf;
-    return Element::create(s);
-}
-
-// XXXMLG currently identical to decode_blob
-ElementPtr
-decode_utf8(std::stringstream& in, int& item_length) {
-    char *buf = new char[item_length + 1];
-
-    in.read(buf, item_length);
-    if (in.fail()) {
-        delete[] buf;
-        throw DecodeError();
-    }
-    buf[item_length] = 0;
-
-    std::string s = std::string(buf, item_length);
-    item_length -= item_length;
-
-    delete [] buf;
-    return Element::create(s);
+    return Element::create(std::string(&buf[0], item_length));
 }
 }
 
 
 ElementPtr
 ElementPtr
@@ -810,7 +786,8 @@ decode_element(std::stringstream& in, int& in_length) {
         element = decode_blob(in, item_length);
         element = decode_blob(in, item_length);
         break;
         break;
     case ITEM_UTF8:
     case ITEM_UTF8:
-        element = decode_utf8(in, item_length);
+        // XXXMLG currently identical to decode_blob
+        element = decode_blob(in, item_length);
         break;
         break;
     case ITEM_HASH:
     case ITEM_HASH:
         element = decode_hash(in, item_length);
         element = decode_hash(in, item_length);