[Groonga-commit] groonga/grnxx at 1fd5bea [master] Fix a bug of empty string creation.

Back to archive index

susumu.yata null+****@clear*****
Tue Dec 16 10:58:51 JST 2014


susumu.yata	2014-11-29 14:51:43 +0900 (Sat, 29 Nov 2014)

  New Revision: 1fd5beabcad7d51b6ac9c19c840eecbbd40b307c
  https://github.com/groonga/grnxx/commit/1fd5beabcad7d51b6ac9c19c840eecbbd40b307c

  Message:
    Fix a bug of empty string creation.

  Modified files:
    lib/grnxx/string.cpp

  Modified: lib/grnxx/string.cpp (+17 -9)
===================================================================
--- lib/grnxx/string.cpp    2014-11-28 17:26:13 +0900 (1d6cfc3)
+++ lib/grnxx/string.cpp    2014-11-29 14:51:43 +0900 (2268e7a)
@@ -24,11 +24,15 @@ String::String(size_t size)
     : buffer_(),
       size_(),
       capacity_() {
-  char *new_buffer = static_cast<char *>(std::malloc(size));
-  if (!new_buffer) {
-    throw "Failed";  // TODO
+  if (size != 0) {
+    char *new_buffer = static_cast<char *>(std::malloc(size));
+    if (!new_buffer) {
+      throw "Memory allocation failed";  // TODO
+    }
+    buffer_ = new_buffer;
+  } else {
+    buffer_ = nullptr;
   }
-  buffer_ = new_buffer;
   size_ = size;
   capacity_ = size;
 }
@@ -37,12 +41,16 @@ String::String(size_t size, char byte)
     : buffer_(),
       size_(),
       capacity_() {
-  char *new_buffer = static_cast<char *>(std::malloc(size));
-  if (!new_buffer) {
-    throw "Failed";  // TODO
+  if (size != 0) {
+    char *new_buffer = static_cast<char *>(std::malloc(size));
+    if (!new_buffer) {
+      throw "Failed";  // TODO
+    }
+    std::memset(new_buffer, byte, size);
+    buffer_ = new_buffer;
+  } else {
+    buffer_ = nullptr;
   }
-  std::memset(new_buffer, byte, size);
-  buffer_ = new_buffer;
   size_ = size;
   capacity_ = size;
 }
-------------- next part --------------
HTML����������������������������...
Descargar 



More information about the Groonga-commit mailing list
Back to archive index