qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject qpid-proton git commit: PROTON-892: pn_data_t capacity does not grow above 32768 items
Date Thu, 20 Aug 2015 16:24:07 GMT
Repository: qpid-proton
Updated Branches:
  refs/heads/master e45e96b53 -> 0ca6ba7e2


PROTON-892: pn_data_t capacity does not grow above 32768 items

Fixed the bug and added a unit test to verify.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/0ca6ba7e
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/0ca6ba7e
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/0ca6ba7e

Branch: refs/heads/master
Commit: 0ca6ba7e261d82ac490ef0ea411d7a6e37de4591
Parents: e45e96b
Author: Alan Conway <aconway@redhat.com>
Authored: Thu Aug 20 12:23:43 2015 -0400
Committer: Alan Conway <aconway@redhat.com>
Committed: Thu Aug 20 12:23:43 2015 -0400

----------------------------------------------------------------------
 proton-c/include/proton/error.h   | 15 +++++++++++
 proton-c/src/codec/codec.c        | 10 ++++---
 proton-c/src/codec/data.h         |  2 ++
 proton-c/src/tests/CMakeLists.txt |  1 +
 proton-c/src/tests/data.c         | 48 ++++++++++++++++++++++++++++++++++
 5 files changed, 73 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0ca6ba7e/proton-c/include/proton/error.h
----------------------------------------------------------------------
diff --git a/proton-c/include/proton/error.h b/proton-c/include/proton/error.h
index 5945af8..94ce449 100644
--- a/proton-c/include/proton/error.h
+++ b/proton-c/include/proton/error.h
@@ -29,6 +29,7 @@
 extern "C" {
 #endif
 
+/** A pn_error_t has an int error `code` and some string `text` to describe the error */

 typedef struct pn_error_t pn_error_t;
 
 #define PN_OK (0)
@@ -43,16 +44,30 @@ typedef struct pn_error_t pn_error_t;
 #define PN_INPROGRESS (-9)
 #define PN_OUT_OF_MEMORY (-10)
 
+/** @return name of the error code. Returned pointer is to a static constant, do not delete.*/

 PN_EXTERN const char *pn_code(int code);
 
 PN_EXTERN pn_error_t *pn_error(void);
 PN_EXTERN void pn_error_free(pn_error_t *error);
+
+/** Reset the error to a "no error" state with code == 0. */
 PN_EXTERN void pn_error_clear(pn_error_t *error);
+
+/** Set the error code and text. Makes a copy of text. */
 PN_EXTERN int pn_error_set(pn_error_t *error, int code, const char *text);
+
+/** Set the code and set the text using a printf-style formatted string. */
 PN_EXTERN int pn_error_vformat(pn_error_t *error, int code, const char *fmt, va_list ap);
+
+/** Set the code and set the text using a printf-style formatted string. */
 PN_EXTERN int pn_error_format(pn_error_t *error, int code, const char *fmt, ...);
+
+/** @return the error code. */
 PN_EXTERN int pn_error_code(pn_error_t *error);
+
+/** @return the error text.  Returned pointer is owned by the pn_error_t. */
 PN_EXTERN const char *pn_error_text(pn_error_t *error);
+
 PN_EXTERN int pn_error_copy(pn_error_t *error, pn_error_t *src);
 
 #define PN_RETURN_IF_ERROR(x) \

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0ca6ba7e/proton-c/src/codec/codec.c
----------------------------------------------------------------------
diff --git a/proton-c/src/codec/codec.c b/proton-c/src/codec/codec.c
index 67d6245..0c4eab9 100644
--- a/proton-c/src/codec/codec.c
+++ b/proton-c/src/codec/codec.c
@@ -417,10 +417,14 @@ void pn_data_clear(pn_data_t *data)
 
 static int pni_data_grow(pn_data_t *data)
 {
-  pni_nid_t new_capacity = 2 * (data->capacity ? data->capacity : 2);
-  pni_node_t *new_nodes = (pni_node_t *)realloc(data->nodes, new_capacity * sizeof(pni_node_t));
+  size_t capacity = data->capacity ? data->capacity : 2;
+  if (capacity >= PNI_NID_MAX) return PN_OUT_OF_MEMORY;
+  else if (capacity < PNI_NID_MAX/2) capacity *= 2;
+  else capacity = PNI_NID_MAX;
+
+  pni_node_t *new_nodes = (pni_node_t *)realloc(data->nodes, capacity * sizeof(pni_node_t));
   if (new_nodes == NULL) return PN_OUT_OF_MEMORY;
-  data->capacity = new_capacity;
+  data->capacity = capacity;
   data->nodes = new_nodes;
   return 0;
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0ca6ba7e/proton-c/src/codec/data.h
----------------------------------------------------------------------
diff --git a/proton-c/src/codec/data.h b/proton-c/src/codec/data.h
index 56e789b..94dc7d6 100644
--- a/proton-c/src/codec/data.h
+++ b/proton-c/src/codec/data.h
@@ -22,11 +22,13 @@
  *
  */
 
+#include <proton/codec.h>
 #include "buffer.h"
 #include "decoder.h"
 #include "encoder.h"
 
 typedef uint16_t pni_nid_t;
+#define PNI_NID_MAX ((pni_nid_t)-1)
 
 typedef struct {
   char *start;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0ca6ba7e/proton-c/src/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/proton-c/src/tests/CMakeLists.txt b/proton-c/src/tests/CMakeLists.txt
index ac749e1..29e3cf6 100644
--- a/proton-c/src/tests/CMakeLists.txt
+++ b/proton-c/src/tests/CMakeLists.txt
@@ -45,3 +45,4 @@ pn_add_c_test (c-parse-url-tests parse-url.c)
 pn_add_c_test (c-refcount-tests refcount.c)
 pn_add_c_test (c-reactor-tests reactor.c)
 pn_add_c_test (c-event-tests event.c)
+pn_add_c_test (c-data-tests data.c)

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/0ca6ba7e/proton-c/src/tests/data.c
----------------------------------------------------------------------
diff --git a/proton-c/src/tests/data.c b/proton-c/src/tests/data.c
new file mode 100644
index 0000000..9994788
--- /dev/null
+++ b/proton-c/src/tests/data.c
@@ -0,0 +1,48 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#undef NDEBUG                   /* Make sure that assert() is enabled even in a release build.
*/
+
+#include <proton/codec.h>
+#include "../codec/data.h"
+#include <assert.h>
+#include <stdio.h>
+
+// Make sure we can grow the capacity of a pn_data_t all the way to the max and we stop there.
+static void test_grow(void)
+{
+  pn_data_t* data = pn_data(0);
+  while (pn_data_size(data) < PNI_NID_MAX) {
+    int code = pn_data_put_int(data, 1);
+    if (code) fprintf(stderr, "%d: %s", code, pn_error_text(pn_data_error(data)));
+    assert(code == 0);
+  }
+  assert(pn_data_size(data) == PNI_NID_MAX);
+  int code = pn_data_put_int(data, 1);
+  if (code != PN_OUT_OF_MEMORY)
+    fprintf(stderr, "expected PN_OUT_OF_MEMORY, got  %s\n", pn_code(code));
+  assert(code == PN_OUT_OF_MEMORY);
+  assert(pn_data_size(data) == PNI_NID_MAX);
+}
+
+int main(int argc, char **argv) {
+  test_grow();
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message