You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2020/07/29 14:10:34 UTC

[qpid-dispatch] 03/06: DISPATCH-1568 Add terminus_private.h to make the `static` method testable

This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git

commit 7c238efe971d611dc4e8d0a7cfea5078df8c0e79
Author: Jiri Danek <jd...@redhat.com>
AuthorDate: Thu Jul 23 10:06:58 2020 +0200

    DISPATCH-1568 Add terminus_private.h to make the `static` method testable
---
 src/router_core/terminus.c | 34 +---------------------------
 src/terminus_private.h     | 55 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/src/router_core/terminus.c b/src/router_core/terminus.c
index 5a7eaf9..b6bb209 100644
--- a/src/router_core/terminus.c
+++ b/src/router_core/terminus.c
@@ -18,13 +18,9 @@
  */
 
 #include "router_core_private.h"
+#include "terminus_private.h"
 #include <strings.h>
-#include <stdio.h>
 #include <inttypes.h>
-#include <limits.h>
-
-#include <qpid/dispatch/macros.h>
-
 
 ALLOC_DEFINE(qdr_terminus_t);
 
@@ -79,34 +75,6 @@ void qdr_terminus_free(qdr_terminus_t *term)
     free_qdr_terminus_t(term);
 }
 
-// DISPATCH-1461: snprintf() is evil - it returns >= size on overflow.  This
-// wrapper will never return >= size, even if truncated.  This makes it safe to
-// do pointer & length arithmetic without overflowing the destination buffer in
-// qdr_terminus_format()
-STATIC INLINE size_t safe_snprintf(char *str, size_t size, const char *format, ...) {
-    // max size allowed must be INT_MAX (since vsnprintf returns an int)
-    if (size == 0 || size > INT_MAX) {
-        return 0;
-    }
-    int max_possible_return_value = (int)(size - 1);
-    va_list ap;
-    va_start(ap, format);
-    int rc = vsnprintf(str, size, format, ap);
-    va_end(ap);
-
-    if (rc < 0) {
-        if (size > 0 && str) {
-            *str = 0;
-        }
-        return 0;
-    }
-
-    if (rc > max_possible_return_value) {
-        rc = max_possible_return_value;
-    }
-    return (size_t)rc;
-}
-
 void qdr_terminus_format(qdr_terminus_t *term, char *output, size_t *size)
 {
     size_t len = safe_snprintf(output, *size, "{");
diff --git a/src/terminus_private.h b/src/terminus_private.h
new file mode 100644
index 0000000..8757ea3
--- /dev/null
+++ b/src/terminus_private.h
@@ -0,0 +1,55 @@
+#ifndef __terminus_private_h__
+#define __terminus_private_h__ 1
+/*
+ * 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.
+ */
+
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+// DISPATCH-1461: snprintf() is evil - it returns >= size on overflow.  This
+// wrapper will never return >= size, even if truncated.  This makes it safe to
+// do pointer & length arithmetic without overflowing the destination buffer in
+// qdr_terminus_format()
+//
+static inline size_t safe_snprintf(char *str, size_t size, const char *format, ...) {
+    // max size allowed must be INT_MAX (since vsnprintf returns an int)
+    if (size == 0 || size > INT_MAX) {
+        return 0;
+    }
+    int max_possible_return_value = (int)(size - 1);
+    va_list ap;
+    va_start(ap, format);
+    int rc = vsnprintf(str, size, format, ap);
+    va_end(ap);
+
+    if (rc < 0) {
+        if (size > 0 && str) {
+            *str = 0;
+        }
+        return 0;
+    }
+
+    if (rc > max_possible_return_value) {
+        rc = max_possible_return_value;
+    }
+    return (size_t)rc;
+}
+
+#endif  // __terminus_private_h__


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