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/31 07:19:27 UTC
[qpid-dispatch] branch master updated: DISPATCH-960 Handle invalid
configs when resolving port (#802)
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
The following commit(s) were added to refs/heads/master by this push:
new 13ea603 DISPATCH-960 Handle invalid configs when resolving port (#802)
13ea603 is described below
commit 13ea6033647bfc177cd0689541ce40b28568c3a6
Author: Jiri Daněk <jd...@redhat.com>
AuthorDate: Fri Jul 31 09:19:18 2020 +0200
DISPATCH-960 Handle invalid configs when resolving port (#802)
---
src/amqp.c | 52 ++++++++++++++++++++++++-----------------
tests/c_unittests/test_amqp.cpp | 48 +++++++++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 22 deletions(-)
diff --git a/src/amqp.c b/src/amqp.c
index 33fff42..dbe1885 100644
--- a/src/amqp.c
+++ b/src/amqp.c
@@ -19,11 +19,10 @@
#include <qpid/dispatch/amqp.h>
#include <errno.h>
+#include <netdb.h>
#include <stdlib.h>
#include <string.h>
-#include <netdb.h>
#include <sys/types.h>
-#include <sys/socket.h>
const char * const QD_MA_PREFIX = "x-opt-qd.";
const char * const QD_MA_INGRESS = "x-opt-qd.ingress";
@@ -96,27 +95,36 @@ const char * const QD_AMQPS_PORT_STR = "5671";
const char * const QD_AMQP_DFLT_PROTO = "tcp";
-int qd_port_int(const char* port_str) {
+int qd_port_int(const char *port_str) {
+ char *endptr;
+ unsigned long n;
+
+ // empty string?
+ if (*port_str == '\0') return -1;
+
+ // digits from beginning to end?
errno = 0;
- unsigned long n = strtoul(port_str, NULL, 10);
- if (errno || n > 0xFFFF) return -1;
-
- // Port is not an integer (port = 'amqp' or 'amqps')
- if ( !n && strlen(port_str) > 0 ) {
- // Resolve service port
- struct servent serv_info;
- struct servent *serv_info_res;
- int buf_len = 4096;
- char *buf = calloc(buf_len, sizeof(char));
-
- // Service port is resolved
- if ( !getservbyname_r(port_str, QD_AMQP_DFLT_PROTO, &serv_info, buf, buf_len, &serv_info_res) ) {
- n = ntohs(serv_info.s_port);
- } else {
- n = -1;
- }
- free(buf);
+ n = strtoul(port_str, &endptr, 10);
+ if (*endptr == '\0') {
+ if (!errno && n >= 0 && n <= 0xFFFF)
+ return n;
+ else
+ return -1;
}
- return n;
+ // digits halfway?
+ if (endptr != port_str) return -1;
+
+ // resolve service port
+ struct servent serv_info;
+ struct servent *serv_info_res;
+ enum { buf_len = 4096 };
+ char buf[buf_len];
+
+ int r = getservbyname_r(port_str, QD_AMQP_DFLT_PROTO, &serv_info, buf, buf_len, &serv_info_res);
+ if (r == 0 && serv_info_res != NULL) {
+ return ntohs(serv_info.s_port);
+ } else {
+ return -1;
+ }
}
diff --git a/tests/c_unittests/test_amqp.cpp b/tests/c_unittests/test_amqp.cpp
new file mode 100644
index 0000000..3dfd2b5
--- /dev/null
+++ b/tests/c_unittests/test_amqp.cpp
@@ -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.
+ */
+
+#include "qdr_doctest.h"
+
+extern "C" {
+#include <qpid/dispatch/amqp.h>
+}
+
+TEST_CASE("test_qd_port_int") {
+ SUBCASE("numeric ports, edge cases") {
+ CHECK(qd_port_int("-1") == -1);
+ CHECK(qd_port_int("0") == 0);
+ CHECK(qd_port_int("1") == 1);
+ CHECK(qd_port_int("5672") == 5672);
+ CHECK(qd_port_int("65535") == 65535);
+ CHECK(qd_port_int("65536") == -1);
+ }
+ SUBCASE("well known symbolic ports") {
+ CHECK(qd_port_int("amqp") == 5672);
+ CHECK(qd_port_int("amqps") == 5671);
+ CHECK(qd_port_int("http") == 80);
+ }
+ SUBCASE("invalid inputs") {
+ CHECK(qd_port_int("") == -1);
+
+ CHECK(qd_port_int("42http") == -1);
+ CHECK(qd_port_int("http42") == -1);
+
+ CHECK(qd_port_int("no_such_port") == -1);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org