Skip to content

Commit

Permalink
use strnlen_s instead of strnlen if available
Browse files Browse the repository at this point in the history
  • Loading branch information
razr committed Sep 15, 2023
1 parent 629d554 commit 612f568
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/error_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

#include <rcutils/allocator.h>
Expand Down Expand Up @@ -198,7 +199,11 @@ rcutils_set_error_state(
error_state.line_number = line_number;
#if RCUTILS_REPORT_ERROR_HANDLING_ERRORS
// Only warn of overwritting if the new error is different from the old ones.
#ifdef __STDC_LIB_EXT1__
size_t characters_to_compare = strnlen_s(error_string, RCUTILS_ERROR_MESSAGE_MAX_LENGTH);
#else
size_t characters_to_compare = strnlen(error_string, RCUTILS_ERROR_MESSAGE_MAX_LENGTH);
#endif
// assumption is that message length is <= max error string length
static_assert(
sizeof(gtls_rcutils_error_state.message) <= sizeof(gtls_rcutils_error_string.str),
Expand Down
5 changes: 5 additions & 0 deletions src/error_handling_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#endif
#include <assert.h>
#include <stdlib.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

#include <rcutils/error_handling.h>
Expand Down Expand Up @@ -130,7 +131,11 @@ __rcutils_convert_uint64_t_into_c_str(uint64_t number, char * buffer, size_t buf
buffer[i] = '\0';

// reverse the string in place
#ifdef __STDC_LIB_EXT1__
__rcutils_reverse_str(buffer, strnlen_s(buffer, 21));
#else
__rcutils_reverse_str(buffer, strnlen(buffer, 21));
#endif
}

// do not use externally, internal function which is only to be used by error_handling.c
Expand Down

0 comments on commit 612f568

Please sign in to comment.