qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cliffjan...@apache.org
Subject svn commit: r1441872 - in /qpid/proton/trunk/proton-c: CMakeLists.txt examples/messenger/c/recv.c src/codec/codec.c src/engine/engine.c src/messenger.c src/proton.c src/sasl/sasl.c src/util.c
Date Sun, 03 Feb 2013 08:22:00 GMT
Author: cliffjansen
Date: Sun Feb  3 08:22:00 2013
New Revision: 1441872

URL: http://svn.apache.org/viewvc?rev=1441872&view=rev
Log:
PROTON-159: C++ support part 4.  See https://reviews.apache.org/r/9088/

Modified:
    qpid/proton/trunk/proton-c/CMakeLists.txt
    qpid/proton/trunk/proton-c/examples/messenger/c/recv.c
    qpid/proton/trunk/proton-c/src/codec/codec.c
    qpid/proton/trunk/proton-c/src/engine/engine.c
    qpid/proton/trunk/proton-c/src/messenger.c
    qpid/proton/trunk/proton-c/src/proton.c
    qpid/proton/trunk/proton-c/src/sasl/sasl.c
    qpid/proton/trunk/proton-c/src/util.c

Modified: qpid/proton/trunk/proton-c/CMakeLists.txt
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/CMakeLists.txt?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/CMakeLists.txt (original)
+++ qpid/proton/trunk/proton-c/CMakeLists.txt Sun Feb  3 08:22:00 2013
@@ -187,7 +187,7 @@ if (CMAKE_COMPILER_IS_GNUCC)
   if (ENABLE_WARNING_ERROR)
     set (WERROR "-Werror")
   endif (ENABLE_WARNING_ERROR)
-  set (COMPILE_WARNING_FLAGS "${WERROR} -Wall -pedantic-errors -Wc++-compat -Wwrite-strings
-Wsign-compare")
+  set (COMPILE_WARNING_FLAGS "${WERROR} -Wall -pedantic-errors -Wc++-compat -Wwrite-strings
-Wsign-compare -Wvla")
   set (COMPILE_LANGUAGE_FLAGS "-std=c99")
   set (COMPILE_PLATFORM_FLAGS "-std=gnu99")
   if (ENABLE_UNDEFINED_ERROR)

Modified: qpid/proton/trunk/proton-c/examples/messenger/c/recv.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/examples/messenger/c/recv.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/examples/messenger/c/recv.c (original)
+++ qpid/proton/trunk/proton-c/examples/messenger/c/recv.c Sun Feb  3 08:22:00 2013
@@ -135,8 +135,8 @@ int main(int argc, char** argv)
       pn_messenger_get(messenger, message);
       check(messenger);
 
-      size_t buffsize = 1024;
-      char buffer[buffsize];
+      char buffer[1024];
+      size_t buffsize = sizeof(buffer);
       pn_data_t *body = pn_message_body(message);
       pn_data_format(body, buffer, &buffsize);
 

Modified: qpid/proton/trunk/proton-c/src/codec/codec.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/codec/codec.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/codec/codec.c (original)
+++ qpid/proton/trunk/proton-c/src/codec/codec.c Sun Feb  3 08:22:00 2013
@@ -141,23 +141,16 @@ int pn_bytes_format(pn_bytes_t *bytes, c
 
 int pn_print_atom(pn_iatom_t atom)
 {
-  size_t size = 4;
-  while (true) {
-    char buf[size];
-    pn_bytes_t bytes = pn_bytes(size, buf);
-    int err = pn_format_atom(&bytes, atom);
-    if (err) {
-      if (err == PN_OVERFLOW) {
-        size *= 2;
-        continue;
-      } else {
-        return err;
-      }
-    } else {
-      printf("%.*s", (int) (size - bytes.size), buf);
-      return 0;
-    }
-  }
+  char buf[256];
+  unsigned size = 256;
+  pn_bytes_t bytes = pn_bytes(size, buf);
+  int err = pn_format_atom(&bytes, atom);
+  if (err && err != PN_OVERFLOW)
+    return err;
+  printf("%.*s", (int) (size - bytes.size), buf);
+  if (err)
+    printf("... (truncated)");
+  return 0;
 }
 
 int pn_format_atom(pn_bytes_t *bytes, pn_iatom_t atom)
@@ -400,24 +393,15 @@ ssize_t pn_format_atoms(char *buf, size_
 
 int pn_print_atoms(const pn_atoms_t *atoms)
 {
-  int size = 128;
-
-  while (true)
-  {
-    char buf[size];
-    ssize_t n = pn_format_atoms(buf, size, *atoms);
-    if (n < 0) {
-      if (n == PN_OVERFLOW) {
-        size *= 2;
-        continue;
-      } else {
-        return n;
-      }
-    } else {
-      printf("%.*s", (int) n, buf);
-      return 0;
-    }
-  }
+  char buf[512];
+  memset(buf, 0, sizeof(buf));
+  ssize_t n = pn_format_atoms(buf, sizeof(buf) - 1, *atoms);
+  if (n < 0 && n != PN_OVERFLOW)
+    return n;
+  printf("%s", buf);
+  if (n == PN_OVERFLOW)
+    printf("... (truncated)");
+  return 0;
 }
 
 int pn_decode_atom(pn_bytes_t *bytes, pn_atoms_t *atoms);
@@ -991,6 +975,8 @@ struct pn_data_t {
   size_t size;
   pn_node_t *nodes;
   pn_buffer_t *buf;
+  pn_iatom_t *iatoms;
+  size_t iatom_capacity;
   size_t parent;
   size_t current;
   size_t base_parent;
@@ -1011,6 +997,8 @@ pn_data_t *pn_data(size_t capacity)
   data->size = 0;
   data->nodes = capacity ? (pn_node_t *) malloc(capacity * sizeof(pn_node_t)) : NULL;
   data->buf = pn_buffer(64);
+  data->iatoms = 0;
+  data->iatom_capacity = 0;
   data->parent = 0;
   data->current = 0;
   data->base_parent = 0;
@@ -1026,6 +1014,7 @@ void pn_data_free(pn_data_t *data)
     free(data->nodes);
     pn_buffer_free(data->buf);
     pn_error_free(data->error);
+    free(data->iatoms);
     free(data);
   }
 }
@@ -1730,16 +1719,18 @@ int pn_data_as_atoms(pn_data_t *data, pn
 
 int pn_data_print(pn_data_t *data)
 {
-  pn_iatom_t atoms[data->size + data->extras];
-  pn_atoms_t latoms = {.size=data->size + data->extras, .start=atoms};
+  size_t count = data->size + data->extras;
+  PN_ENSURE(data->iatoms, data->iatom_capacity, count, pn_iatom_t);
+  pn_atoms_t latoms = {count, data->iatoms};
   pn_data_as_atoms(data, &latoms);
   return pn_print_atoms(&latoms);
 }
 
 int pn_data_format(pn_data_t *data, char *bytes, size_t *size)
 {
-  pn_iatom_t atoms[data->size + data->extras];
-  pn_atoms_t latoms = {.size=data->size + data->extras, .start=atoms};
+  size_t count = data->size + data->extras;
+  PN_ENSURE(data->iatoms, data->iatom_capacity, count, pn_iatom_t);
+  pn_atoms_t latoms = {count, data->iatoms};
   pn_data_as_atoms(data, &latoms);
 
   ssize_t sz = pn_format_atoms(bytes, *size, latoms);
@@ -2410,9 +2401,9 @@ ssize_t pn_data_decode(pn_data_t *data, 
   pn_bytes_t lbytes;
 
   while (true) {
-    pn_iatom_t atoms[asize];
+    PN_ENSURE(data->iatoms, data->iatom_capacity, asize, pn_iatom_t);
     latoms.size = asize;
-    latoms.start = atoms;
+    latoms.start = data->iatoms;
     lbytes.size = size;
     lbytes.start = (char *)bytes;  // PROTON-77
 

Modified: qpid/proton/trunk/proton-c/src/engine/engine.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/engine/engine.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/engine/engine.c (original)
+++ qpid/proton/trunk/proton-c/src/engine/engine.c Sun Feb  3 08:22:00 2013
@@ -1506,7 +1506,9 @@ int pn_do_attach(pn_dispatcher_t *disp)
                          &target, &tgt_dr, &tgt_exp, &tgt_timeout, &tgt_dynamic,
                          &idc);
   if (err) return err;
-  char strname[name.size + 1];
+  char strbuf[128];      // avoid malloc for most link names
+  char *strheap = (name.size >= sizeof(strbuf)) ? (char *) malloc(name.size + 1) : NULL;
+  char *strname = strheap ? strheap : strbuf;
   strncpy(strname, name.start, name.size);
   strname[name.size] = '\0';
 
@@ -1524,6 +1526,10 @@ int pn_do_attach(pn_dispatcher_t *disp)
     link = link_state->link;
   }
 
+  if (strheap) {
+    free(strheap);
+  }
+
   pn_map_handle(ssn_state, handle, link_state);
   PN_SET_REMOTE(link->endpoint.state, PN_REMOTE_ACTIVE);
   pn_terminus_t *rsrc = &link_state->link->remote_source;

Modified: qpid/proton/trunk/proton-c/src/messenger.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/messenger.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/messenger.c (original)
+++ qpid/proton/trunk/proton-c/src/messenger.c Sun Feb  3 08:22:00 2013
@@ -710,7 +710,8 @@ static const char *default_port(const ch
 }
 pn_connection_t *pn_messenger_resolve(pn_messenger_t *messenger, char *address, char **name)
 {
-  char domain[strlen(address) + 1];
+  char domain[256];
+  if (sizeof(domain) < strlen(address) + 1) return NULL;
   char *scheme = NULL;
   char *user = NULL;
   char *pass = NULL;
@@ -782,7 +783,8 @@ void pn_subscription_set_context(pn_subs
 
 pn_link_t *pn_messenger_link(pn_messenger_t *messenger, const char *address, bool sender)
 {
-  char copy[(address ? strlen(address) : 0) + 1];
+  char copy[256];
+  if (sizeof(copy) <= (address ? strlen(address) : 0)) return NULL;
   if (address) {
     strcpy(copy, address);
   } else {
@@ -830,7 +832,8 @@ pn_link_t *pn_messenger_target(pn_messen
 
 pn_subscription_t *pn_messenger_subscribe(pn_messenger_t *messenger, const char *source)
 {
-  char copy[strlen(source) + 1];
+  char copy[256];
+  if (strlen(source) >= sizeof(copy)) return NULL;
   strcpy(copy, source);
 
   char *scheme = NULL;
@@ -916,17 +919,29 @@ int pn_messenger_set_incoming_window(pn_
 
 static void outward_munge(pn_messenger_t *mng, pn_message_t *msg)
 {
+  char stackbuf[256];
+  char *heapbuf = NULL;
+  char *buf = stackbuf;
   const char *address = pn_message_get_reply_to(msg);
   int len = address ? strlen(address) : 0;
   if (len > 1 && address[0] == '~' && address[1] == '/') {
-    char buf[len + strlen(mng->name) + 9];
+    unsigned needed = len + strlen(mng->name) + 9;
+    if (needed > sizeof(stackbuf)) {
+      heapbuf = (char *) malloc(needed);
+      buf = heapbuf;
+    }
     sprintf(buf, "amqp://%s/%s", mng->name, address + 2);
     pn_message_set_reply_to(msg, buf);
   } else if (len == 0) {
-    char buf[strlen(mng->name) + 8];
+    unsigned needed = strlen(mng->name) + 8;
+    if (needed > sizeof(stackbuf)) {
+      heapbuf = (char *) malloc(needed);
+      buf = heapbuf;
+    }
     sprintf(buf, "amqp://%s", mng->name);
     pn_message_set_reply_to(msg, buf);
   }
+  if (heapbuf) free (heapbuf);
 }
 
 // static bool false_pred(pn_messenger_t *messenger) { return false; }

Modified: qpid/proton/trunk/proton-c/src/proton.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/proton.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/proton.c (original)
+++ qpid/proton/trunk/proton-c/src/proton.c Sun Feb  3 08:22:00 2013
@@ -39,6 +39,19 @@
 #include <inttypes.h>
 #include "protocol.h"
 
+typedef struct {
+  char *buf;
+  size_t capacity;
+} heap_buffer;
+
+heap_buffer client_msg, client_data, server_data, server_iresp;
+void free_heap_buffers() {
+  free(client_msg.buf);
+  free(client_data.buf);
+  free(server_data.buf);
+  free(server_iresp.buf);
+}
+
 int buffer(int argc, char **argv)
 {
   pn_buffer_t *buf = pn_buffer(16);
@@ -112,7 +125,8 @@ void server_callback(pn_connector_t *cto
     case PN_SASL_STEP:
       {
         size_t n = pn_sasl_pending(sasl);
-        char iresp[n];
+        PN_ENSURE(server_iresp.buf, server_iresp.capacity, n, char);
+        char *iresp = server_iresp.buf;
         pn_sasl_recv(sasl, iresp, n);
         printf("%s", pn_sasl_remote_mechanisms(sasl));
         printf(" response = ");
@@ -133,7 +147,8 @@ void server_callback(pn_connector_t *cto
   struct server_context *ctx = (struct server_context *) pn_connector_context(ctor);
   char tagstr[1024];
   char msg[10*1024];
-  char data[ctx->size + 16];
+  PN_ENSURE(server_data.buf, server_data.capacity, (size_t) ctx->size + 16, char);
+  char *data = server_data.buf;
   for (int i = 0; i < ctx->size; i++) {
     msg[i] = 'x';
   }
@@ -279,8 +294,10 @@ void client_callback(pn_connector_t *cto
 
   pn_connection_t *connection = pn_connector_connection(ctor);
   char tagstr[1024];
-  char msg[ctx->size];
-  char data[ctx->size + 16];
+  PN_ENSURE(client_msg.buf, client_msg.capacity, (size_t) ctx->size, char);
+  char *msg = client_msg.buf;
+  PN_ENSURE(client_data.buf, client_data.capacity, (size_t) ctx->size + 16, char);
+  char *data = client_data.buf;
   for (int i = 0; i < ctx->size; i++) {
     msg[i] = 'x';
   }
@@ -508,6 +525,7 @@ int main(int argc, char **argv)
   }
 
   pn_driver_free(drv);
+  free_heap_buffers();
 
   return 0;
 }

Modified: qpid/proton/trunk/proton-c/src/sasl/sasl.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/sasl/sasl.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/sasl/sasl.c (original)
+++ qpid/proton/trunk/proton-c/src/sasl/sasl.c Sun Feb  3 08:22:00 2013
@@ -194,7 +194,7 @@ void pn_sasl_plain(pn_sasl_t *sasl, cons
   size_t usize = strlen(user);
   size_t psize = strlen(pass);
   size_t size = usize + psize + 2;
-  char iresp[size];
+  char *iresp = (char *) malloc(size);
 
   iresp[0] = 0;
   memmove(iresp + 1, user, usize);
@@ -204,6 +204,7 @@ void pn_sasl_plain(pn_sasl_t *sasl, cons
   pn_sasl_mechanisms(sasl, "PLAIN");
   pn_sasl_send(sasl, iresp, size);
   pn_sasl_client(sasl);
+  free(iresp);
 }
 
 void pn_sasl_done(pn_sasl_t *sasl, pn_sasl_outcome_t outcome)

Modified: qpid/proton/trunk/proton-c/src/util.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/util.c?rev=1441872&r1=1441871&r2=1441872&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/util.c (original)
+++ qpid/proton/trunk/proton-c/src/util.c Sun Feb  3 08:22:00 2013
@@ -62,13 +62,17 @@ ssize_t pn_quote_data(char *dst, size_t 
 
 void pn_fprint_data(FILE *stream, const char *bytes, size_t size)
 {
-  size_t capacity = 4*size + 1;
-  char buf[capacity];
-  ssize_t n = pn_quote_data(buf, capacity, bytes, size);
+  char buf[256];
+  ssize_t n = pn_quote_data(buf, 256, bytes, size);
   if (n >= 0) {
     fputs(buf, stream);
   } else {
-    fprintf(stderr, "pn_quote_data: %zi\n", n);
+    if (n == PN_OVERFLOW) {
+      fputs(buf, stream);
+      fputs("... (truncated)", stream);
+    }
+    else
+      fprintf(stderr, "pn_quote_data: %s\n", pn_code(n));
   }
 }
 



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


Mime
View raw message