Skip to content

Commit

Permalink
python: rewrite oc_python.c to avoid using strcat
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jul 24, 2023
1 parent 0704dac commit 1b8a938
Show file tree
Hide file tree
Showing 36 changed files with 401 additions and 150 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,11 @@ if(BUILD_TESTING AND(UNIX OR MINGW))
oc_package_add_test(TARGET cloudtest SOURCES ${COMMONTEST_SRC} ${CLOUDTEST_SRC})
endif()

if(OC_SECURITY_ENABLED AND OC_DYNAMIC_ALLOCATION_ENABLED)
file(GLOB PYTHONLIBTEST_SRC python/oc_python.c python/unittest/*.cpp)
oc_package_add_test(TARGET pythonlibtest SOURCES ${PYTHONLIBTEST_SRC})
endif()

if(UNIX)
# install https://github.com/wolfcw/libfaketime for this test suit to run
find_library(FAKETIME_LIBRARY
Expand Down
2 changes: 1 addition & 1 deletion api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#ifdef OC_CLIENT

#include "api/oc_buffer_internal.h"
#include "api/oc_helpers_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/client/oc_client_cb_internal.h"
#include "messaging/coap/coap.h"
#include "messaging/coap/transactions.h"
Expand Down
2 changes: 1 addition & 1 deletion api/oc_buffer.c → api/oc_message_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

#include "messaging/coap/engine.h"
#include "oc_buffer.h"
#include "oc_buffer_internal.h"
#include "oc_config.h"
#include "oc_events_internal.h"
#include "oc_message_buffer_internal.h"
#include "oc_signal_event_loop.h"
#include "port/oc_network_event_handler_internal.h"
#include "util/oc_features.h"
Expand Down
6 changes: 3 additions & 3 deletions api/oc_buffer_internal.h → api/oc_message_buffer_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
****************************************************************************/

#ifndef OC_BUFFER_INTERNAL_H
#define OC_BUFFER_INTERNAL_H
#ifndef OC_MESSAGE_BUFFER_INTERNAL_H
#define OC_MESSAGE_BUFFER_INTERNAL_H

#include "api/oc_tcp_internal.h"
#include "port/oc_connectivity.h"
Expand Down Expand Up @@ -100,4 +100,4 @@ void oc_close_all_tls_sessions_for_device(size_t device);
}
#endif

#endif /* OC_BUFFER_INTERNAL_H */
#endif /* OC_MESSAGE_BUFFER_INTERNAL_H */
4 changes: 2 additions & 2 deletions api/oc_network_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
****************************************************************************/

#include "oc_network_events_internal.h"
#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_network_events_internal.h"
#include "messaging/coap/coap.h"
#include "oc_buffer.h"
#include "oc_config.h"
Expand Down
39 changes: 3 additions & 36 deletions api/oc_rep_to_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "oc_base64.h"
#include "oc_rep.h"
#include "port/oc_log_internal.h"
#include "util/oc_buffer_internal.h"
#include "util/oc_compiler.h"
#include <inttypes.h>
#include <stdarg.h>
Expand All @@ -28,40 +29,6 @@
#include <stdint.h>
#include <stdio.h>

typedef struct
{
char *buffer; // the character buffer being updated
size_t buffer_size; // the size of the character buffer being updated.
size_t total; // running total of characters printed to buf
} write_buffer_t;

static long write_to_buffer(write_buffer_t *wb, const char *fmt, ...)
OC_PRINTF_FORMAT(2, 3);

static long
write_to_buffer(write_buffer_t *wb, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
int num_char_printed = vsnprintf(wb->buffer, wb->buffer_size, fmt, args);
va_end(args);
if (num_char_printed < 0) {
return -1;
}
wb->total += num_char_printed;
if (wb->buffer == NULL) {
return (long)wb->total;
}
if ((size_t)num_char_printed < wb->buffer_size) {
wb->buffer += num_char_printed;
wb->buffer_size -= num_char_printed;
} else {
wb->buffer += wb->buffer_size;
wb->buffer_size = 0;
}
return (long)wb->total;
}

/*
* This macro assumes that four variables are already avalible to be changed.
*
Expand Down Expand Up @@ -103,13 +70,13 @@ write_to_buffer(write_buffer_t *wb, const char *fmt, ...)
static size_t
oc_rep_to_json_tab(char *buf, size_t buf_size, int tab_depth)
{
write_buffer_t b = {
oc_write_buffer_t b = {
.buffer = buf,
.buffer_size = buf_size,
.total = 0,
};
for (int i = 0; i < tab_depth; i++) {
if (write_to_buffer(&b, "%s", OC_PRETTY_PRINT_TAB_CHARACTER) < 0) {
if (oc_buffer_write(&b, "%s", OC_PRETTY_PRINT_TAB_CHARACTER) < 0) {
return (size_t)-1;
}
}
Expand Down
10 changes: 5 additions & 5 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*
***************************************************************************/

#include "oc_ri.h"
#include "oc_ri_internal.h"
#include "api/oc_buffer_internal.h"
#include "api/oc_event_callback_internal.h"
#include "api/oc_events_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_network_events_internal.h"
#include "messaging/coap/coap_internal.h"
#include "messaging/coap/constants.h"
#include "messaging/coap/engine.h"
Expand All @@ -28,8 +28,8 @@
#include "oc_buffer.h"
#include "oc_core_res.h"
#include "oc_discovery.h"
#include "oc_events_internal.h"
#include "oc_network_events_internal.h"
#include "oc_ri.h"
#include "oc_ri_internal.h"
#include "oc_uuid.h"
#include "port/oc_assert.h"
#include "port/oc_random.h"
Expand Down
6 changes: 3 additions & 3 deletions api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
*
****************************************************************************/

#include "api/oc_buffer_internal.h"
#include "api/oc_core_res_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_ri_internal.h"
#include "api/oc_server_api_internal.h"
#include "messaging/coap/engine.h"
#include "messaging/coap/oc_coap.h"
#include "messaging/coap/observe.h"
#include "messaging/coap/separate.h"
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_core_res_internal.h"
#include "oc_server_api_internal.h"
#include "port/oc_log_internal.h"
#include "util/oc_features.h"
#include "util/oc_macros_internal.h"
Expand Down
2 changes: 1 addition & 1 deletion api/plgd/plgd_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#ifdef OC_HAS_FEATURE_PLGD_TIME

#include "plgd_time_internal.h"
#include "api/oc_buffer_internal.h"
#include "api/oc_core_res_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_rep_internal.h"
#include "api/oc_resource_internal.h"
#include "api/oc_ri_internal.h"
Expand Down
13 changes: 4 additions & 9 deletions api/unittest/base64test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,16 @@ toString(From *arr, size_t arrSize)

TEST(B64Test, RFC4648_EncodeFail)
{
std::vector<std::string> input = {
std::vector<std::string> inputs = {
"foo",
"foobar",
};

std::vector<size_t> expectedOutputLenth = {
4,
8,
};

for (size_t i = 0; i < input.size(); ++i) {
auto toEncode = fromString<uint8_t>(input[i]);
std::for_each(inputs.begin(), inputs.end(), [](std::string &str) {
auto toEncode = fromString<uint8_t>(str);
EXPECT_EQ(-1,
oc_base64_encode(toEncode.data(), toEncode.size(), nullptr, 0));
}
});
}

/*
Expand Down
2 changes: 1 addition & 1 deletion api/unittest/buffertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
****************************************************************************/

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_ri_internal.h"
#include "oc_buffer.h"
#include "oc_config.h"
Expand Down
3 changes: 1 addition & 2 deletions messaging/coap/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
* This file is part of the Contiki operating system.
*/

#include "api/oc_buffer_internal.h"

#include "api/oc_message_buffer_internal.h"
#include "coap.h"
#include "coap_internal.h"
#include "oc_ri.h"
Expand Down
2 changes: 1 addition & 1 deletion messaging/coap/coap_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
****************************************************************************/

#include "api/oc_buffer_internal.h"
#include "api/oc_helpers_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "coap_signal.h"
#include "coap.h"
#include "transactions.h"
Expand Down
2 changes: 1 addition & 1 deletion messaging/coap/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@

#include "engine.h"

#include "api/oc_buffer_internal.h"
#include "api/oc_helpers_internal.h"
#include "api/oc_events_internal.h"
#include "api/oc_main.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_ri_internal.h"
#include "messaging/coap/coap_internal.h"
#include "oc_api.h"
Expand Down
2 changes: 1 addition & 1 deletion messaging/coap/observe.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

#ifdef OC_SERVER

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_server_api_internal.h"
#include "oc_api.h"
#include "oc_buffer.h"
Expand Down
2 changes: 1 addition & 1 deletion messaging/coap/oscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifdef OC_SECURITY
#ifdef OC_OSCORE

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "oscore.h"
#include "coap.h"
#include "coap_signal.h"
Expand Down
4 changes: 2 additions & 2 deletions messaging/coap/separate.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@

#ifdef OC_SERVER

#include "oc_buffer.h"
#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "messaging/coap/coap_internal.h"
#include "oc_buffer.h"
#include "separate.h"
#include "transactions.h"
#include "util/oc_memb.h"
Expand Down
2 changes: 1 addition & 1 deletion messaging/coap/transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
*/

#include "transactions.h"
#include "api/oc_buffer_internal.h"
#include "api/oc_main.h"
#include "api/oc_message_buffer_internal.h"
#include "observe.h"
#include "oc_buffer.h"
#include "util/oc_list.h"
Expand Down
2 changes: 1 addition & 1 deletion port/android/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define __USE_GNU

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_session_events_internal.h"
#include "api/oc_tcp_internal.h"
#include "ipcontext.h"
Expand Down
2 changes: 1 addition & 1 deletion port/esp32/adapter/src/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define __USE_GNU

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_session_events_internal.h"
#include "api/oc_tcp_internal.h"
#include "ipcontext.h"
Expand Down
5 changes: 3 additions & 2 deletions port/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ set(sources

${CMAKE_CURRENT_SOURCE_DIR}/../../../api/client/oc_client_cb.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_base64.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_blockwise.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_buffer.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_blockwise.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_client_api.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_client_role.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_con_resource.c
Expand All @@ -45,6 +44,7 @@ set(sources
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_link.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_log.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_main.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_message_buffer.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_network_events.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_rep.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_rep_encode.c
Expand All @@ -63,6 +63,7 @@ set(sources
${CMAKE_CURRENT_SOURCE_DIR}/../../../messaging/coap/separate.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../messaging/coap/transactions.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../port/common/oc_ip.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../util/oc_buffer.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../util/oc_etimer.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../util/oc_list.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../util/oc_memb.c
Expand Down
2 changes: 1 addition & 1 deletion port/linux/tcpsession.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define __USE_GNU

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_network_events_internal.h"
#include "api/oc_session_events_internal.h"
#include "api/oc_tcp_internal.h"
Expand Down
2 changes: 1 addition & 1 deletion port/unittest/connectivitytest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
******************************************************************/

#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_tcp_internal.h"
#include "api/oc_session_events_internal.h"
#include "messaging/coap/coap.h"
Expand Down
2 changes: 1 addition & 1 deletion port/windows/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
****************************************************************************/

#define WIN32_LEAN_AND_MEAN
#include "api/oc_buffer_internal.h"
#include "api/oc_message_buffer_internal.h"
#include "api/oc_network_events_internal.h"
#include "api/oc_session_events_internal.h"
#include "api/oc_tcp_internal.h"
Expand Down
3 changes: 2 additions & 1 deletion port/windows/vs2015/IoTivity-lite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@
<ClCompile Include="..\..\..\api\cloud\rd_client.c" />
<ClCompile Include="..\..\..\api\oc_base64.c" />
<ClCompile Include="..\..\..\api\oc_blockwise.c" />
<ClCompile Include="..\..\..\api\oc_buffer.c" />
<ClCompile Include="..\..\..\api\oc_client_api.c" />
<ClCompile Include="..\..\..\api\oc_clock.c" />
<ClCompile Include="..\..\..\api\oc_collection.c" />
Expand All @@ -335,6 +334,7 @@
<ClCompile Include="..\..\..\api\oc_introspection.c" />
<ClCompile Include="..\..\..\api\oc_log.c" />
<ClCompile Include="..\..\..\api\oc_main.c" />
<ClCompile Include="..\..\..\api\oc_message_buffer.c" />
<ClCompile Include="..\..\..\api\oc_mnt.c" />
<ClCompile Include="..\..\..\api\oc_network_events.c" />
<ClCompile Include="..\..\..\api\oc_rep.c" />
Expand Down Expand Up @@ -447,6 +447,7 @@
<ClCompile Include="..\..\..\security\oc_store.c" />
<ClCompile Include="..\..\..\security\oc_svr.c" />
<ClCompile Include="..\..\..\security\oc_tls.c" />
<ClCompile Include="..\..\..\util\oc_buffer.c" />
<ClCompile Include="..\..\..\util\oc_etimer.c" />
<ClCompile Include="..\..\..\util\oc_list.c" />
<ClCompile Include="..\..\..\util\oc_memb.c" />
Expand Down
Loading

0 comments on commit 1b8a938

Please sign in to comment.