Refactor topic check functions to common static library.

This commit is contained in:
Roger A. Light 2024-03-17 08:15:22 +00:00
parent a6344f833b
commit 81ad44e677
18 changed files with 18 additions and 68 deletions

View file

@ -99,4 +99,6 @@ typedef SSIZE_T ssize_t;
# define BROKER_EXPORT
#endif
#define TOPIC_HIERARCHY_LIMIT 200
#endif

View file

@ -48,7 +48,7 @@ set(C_SRC
srv_mosq.c
thread_mosq.c
tls_mosq.c
util_mosq.c util_topic.c util_mosq.h
util_mosq.c util_mosq.h
will_mosq.c will_mosq.h)
if (WITH_THREADING AND WIN32)

View file

@ -81,7 +81,6 @@ OBJS= \
thread_mosq.o \
tls_mosq.o \
util_mosq.o \
util_topic.o \
will_mosq.o
OBJS_EXTERNAL= \

View file

@ -1,6 +1,7 @@
set(C_SRC
strings_common.c
time_common.c
topic_common.c
utf8_common.c
)

View file

@ -14,6 +14,7 @@ LOCAL_LIBADD+=
OBJS= \
strings_common.o \
time_common.o \
topic_common.o \
utf8_common.o
all : libmosquitto_common.a

View file

@ -29,17 +29,7 @@ Contributors:
# include <sys/stat.h>
#endif
#ifdef WITH_BROKER
#include "mosquitto_broker_internal.h"
#endif
#include "mosquitto.h"
#include "memory_mosq.h"
#include "net_mosq.h"
#include "send_mosq.h"
#include "tls_mosq.h"
#include "util_mosq.h"
/* Check that a topic used for publishing is valid.
* Search for + or # in a topic. Return MOSQ_ERR_INVAL if found.
@ -49,9 +39,7 @@ Contributors:
BROKER_EXPORT int mosquitto_pub_topic_check(const char *str)
{
int len = 0;
#ifdef WITH_BROKER
int hier_count = 0;
#endif
if(str == NULL){
return MOSQ_ERR_INVAL;
@ -60,19 +48,14 @@ BROKER_EXPORT int mosquitto_pub_topic_check(const char *str)
while(str && str[0]){
if(str[0] == '+' || str[0] == '#'){
return MOSQ_ERR_INVAL;
}
#ifdef WITH_BROKER
else if(str[0] == '/'){
}else if(str[0] == '/'){
hier_count++;
}
#endif
len++;
str = &str[1];
}
if(len > 65535) return MOSQ_ERR_INVAL;
#ifdef WITH_BROKER
if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
#endif
return MOSQ_ERR_SUCCESS;
}
@ -80,9 +63,7 @@ BROKER_EXPORT int mosquitto_pub_topic_check(const char *str)
BROKER_EXPORT int mosquitto_pub_topic_check2(const char *str, size_t len)
{
size_t i;
#ifdef WITH_BROKER
int hier_count = 0;
#endif
if(str == NULL || len > 65535){
return MOSQ_ERR_INVAL;
@ -91,16 +72,11 @@ BROKER_EXPORT int mosquitto_pub_topic_check2(const char *str, size_t len)
for(i=0; i<len; i++){
if(str[i] == '+' || str[i] == '#'){
return MOSQ_ERR_INVAL;
}
#ifdef WITH_BROKER
else if(str[i] == '/'){
}else if(str[i] == '/'){
hier_count++;
}
#endif
}
#ifdef WITH_BROKER
if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
#endif
return MOSQ_ERR_SUCCESS;
}
@ -116,9 +92,7 @@ BROKER_EXPORT int mosquitto_sub_topic_check(const char *str)
{
char c = '\0';
int len = 0;
#ifdef WITH_BROKER
int hier_count = 0;
#endif
if(str == NULL){
return MOSQ_ERR_INVAL;
@ -133,20 +107,15 @@ BROKER_EXPORT int mosquitto_sub_topic_check(const char *str)
if((c != '\0' && c != '/') || str[1] != '\0'){
return MOSQ_ERR_INVAL;
}
}
#ifdef WITH_BROKER
else if(str[0] == '/'){
}else if(str[0] == '/'){
hier_count++;
}
#endif
len++;
c = str[0];
str = &str[1];
}
if(len > 65535) return MOSQ_ERR_INVAL;
#ifdef WITH_BROKER
if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
#endif
return MOSQ_ERR_SUCCESS;
}
@ -155,9 +124,7 @@ BROKER_EXPORT int mosquitto_sub_topic_check2(const char *str, size_t len)
{
char c = '\0';
size_t i;
#ifdef WITH_BROKER
int hier_count = 0;
#endif
if(str == NULL || len > 65535){
return MOSQ_ERR_INVAL;
@ -172,17 +139,12 @@ BROKER_EXPORT int mosquitto_sub_topic_check2(const char *str, size_t len)
if((c != '\0' && c != '/') || i<len-1){
return MOSQ_ERR_INVAL;
}
}
#ifdef WITH_BROKER
else if(str[i] == '/'){
}else if(str[i] == '/'){
hier_count++;
}
#endif
c = str[i];
}
#ifdef WITH_BROKER
if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
#endif
return MOSQ_ERR_SUCCESS;
}

View file

@ -74,7 +74,7 @@ set (MOSQ_SRCS
sys_tree.c sys_tree.h
../lib/tls_mosq.c
topic_tok.c
../lib/util_mosq.c ../lib/util_topic.c ../lib/util_mosq.h
../lib/util_mosq.c ../lib/util_mosq.h
websockets.c
will_delay.c
../lib/will_mosq.c ../lib/will_mosq.h

View file

@ -138,7 +138,6 @@ OBJS_EXTERNAL= \
send_unsubscribe.o \
tls_mosq.o \
util_mosq.o \
util_topic.o \
will_mosq.o
ifeq ($(WITH_WEBSOCKETS),yes)
@ -235,9 +234,6 @@ tls_mosq.o : ${R}/lib/tls_mosq.c
util_mosq.o : ${R}/lib/util_mosq.c ${R}/lib/util_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
util_topic.o : ${R}/lib/util_topic.c ${R}/lib/util_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
will_mosq.o : ${R}/lib/will_mosq.c ${R}/lib/will_mosq.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@

View file

@ -61,7 +61,6 @@ Contributors:
#define MQTT3_LOG_ALL 0xFF
#define CMD_PORT_LIMIT 10
#define TOPIC_HIERARCHY_LIMIT 200
typedef uint64_t dbid_t;

View file

@ -3,7 +3,6 @@ add_library(bridge-topic-obj
OBJECT
../../../src/bridge_topic.c
../../../lib/memory_mosq.c
../../../lib/util_topic.c
)
target_compile_definitions(bridge-topic-obj PRIVATE WITH_BRIDGE WITH_BROKER)
target_link_libraries(bridge-topic-obj PUBLIC common-unit-test-header)
@ -13,7 +12,7 @@ add_executable(bridge-topic-test
stubs.c
)
target_compile_definitions(bridge-topic-test PRIVATE WITH_BRIDGE WITH_BROKER)
target_link_libraries(bridge-topic-test PRIVATE bridge-topic-obj common-unit-test-header)
target_link_libraries(bridge-topic-test PRIVATE bridge-topic-obj common-unit-test-header libmosquitto_common)
add_test(NAME unit-bridge-topic-test COMMAND bridge-topic-test)
# keepalive-test

View file

@ -21,8 +21,7 @@ BRIDGE_TOPIC_OBJS = \
${R}/src/bridge_topic.o \
${R}/src/memory_mosq.o \
${R}/src/packet_datatypes.o \
${R}/src/property_mosq.o \
${R}/src/util_topic.o
${R}/src/property_mosq.o
KEEPALIVE_TEST_OBJS = \
keepalive_stubs.o \
@ -170,9 +169,6 @@ ${R}/src/topic_tok.o : ${R}/src/topic_tok.c
${R}/src/util_mosq.o : ${R}/lib/util_mosq.c
$(MAKE) -C ${R}/src/ util_mosq.o
${R}/src/util_topic.o : ${R}/lib/util_topic.c
$(MAKE) -C ${R}/src/ util_topic.o
build : bridge_topic_test keepalive_test persist_read_test persist_write_test subs_test
test : build

View file

@ -8,7 +8,6 @@ add_executable(lib-test
property_write.c
property_value.c
stubs.c
util_topic_test.c
# main test files
test.c
../../../lib/memory_mosq.c
@ -17,7 +16,6 @@ add_executable(lib-test
../../../lib/packet_mosq.c
../../../lib/property_mosq.c
../../../lib/util_mosq.c
../../../lib/util_topic.c
)
target_link_libraries(lib-test PRIVATE common-unit-test-header OpenSSL::SSL libmosquitto_common)

View file

@ -23,7 +23,6 @@ TEST_OBJS = \
property_write.o \
property_value.o \
stubs.o \
util_topic_test.o
LIB_OBJS = \
${R}/lib/memory_mosq.o \
@ -31,8 +30,7 @@ LIB_OBJS = \
${R}/lib/packet_datatypes.o \
${R}/lib/packet_mosq.o \
${R}/lib/property_mosq.o \
${R}/lib/util_mosq.o \
${R}/lib/util_topic.o
${R}/lib/util_mosq.o
all : test-compile
@ -66,9 +64,6 @@ ${R}/lib/property_mosq.o : ${R}/lib/property_mosq.c
${R}/lib/util_mosq.o : ${R}/lib/util_mosq.c
$(MAKE) -C ${R}/lib/ util_mosq.o
${R}/lib/util_topic.o : ${R}/lib/util_topic.c
$(MAKE) -C ${R}/lib/ util_topic.o
${R}/lib/utf8_mosq.o : ${R}/common/utf8_mosq.c
$(MAKE) -C ${R}/lib/ utf8_mosq.o

View file

@ -11,7 +11,6 @@ int init_property_read_tests(void);
int init_property_user_read_tests(void);
int init_property_write_tests(void);
int init_property_value_tests(void);
int init_util_topic_tests(void);
int init_misc_trim_tests(void);
int main(int argc, char *argv[])
@ -34,7 +33,6 @@ int main(int argc, char *argv[])
|| init_property_user_read_tests()
|| init_property_write_tests()
|| init_property_value_tests()
|| init_util_topic_tests()
|| init_misc_trim_tests()
){

View file

@ -1,6 +1,7 @@
add_executable(libcommon-test
strings_test.c
test.c
topic_test.c
utf8.c
)

View file

@ -15,6 +15,7 @@ endif
TEST_OBJS = \
strings_test.o \
test.o \
topic_test.o \
utf8.o \
LIB_OBJS =

View file

@ -5,6 +5,7 @@
#include <CUnit/Basic.h>
int init_strings_tests(void);
int init_topic_tests(void);
int init_utf8_tests(void);
int main(int argc, char *argv[])
@ -21,6 +22,7 @@ int main(int argc, char *argv[])
if(0
|| init_strings_tests()
|| init_topic_tests()
|| init_utf8_tests()
){

View file

@ -1458,7 +1458,7 @@ static void TEST_sub_match_acl(void)
* TEST SUITE SETUP
* ======================================================================== */
int init_util_topic_tests(void)
int init_topic_tests(void)
{
CU_pSuite test_suite = NULL;