Skip to content

Commit

Permalink
fix(ndk): bsg_strncpy correctly terminates dst even if src is NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
lemnik committed Mar 23, 2022
1 parent 58b077c commit edcb70f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## TBD

### Bug fixes

* Calling `bugsnag_event_set_context` with NULL `context` correctly clears the event context again
[#1637](https://github.com/bugsnag/bugsnag-android/pull/1637)

## 5.21.0 (2022-03-17)

### Enhancements
Expand Down
6 changes: 4 additions & 2 deletions bugsnag-plugin-android-ndk/src/main/jni/utils/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ size_t bsg_strlen(const char *str) {
}

void bsg_strncpy(char *dst, const char *src, size_t dst_size) {
if (src == NULL || dst == NULL || dst_size == 0) {
if (dst == NULL || dst_size == 0) {
return;
}
dst[0] = '\0';
strncat(dst, src, dst_size - 1);
if (src != NULL) {
strncat(dst, src, dst_size - 1);
}
}
11 changes: 11 additions & 0 deletions bugsnag-plugin-android-ndk/src/test/cpp/test_utils_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ TEST test_copy_empty_string(void) {
PASS();
}

TEST test_copy_null_string(void) {
int dst_len = 10;
char *dst = calloc(sizeof(char), dst_len);
strcpy(dst, "C h a n g e");
bsg_strncpy(dst, NULL, dst_len);
ASSERT(dst[0] == '\0');
free(dst);
PASS();
}

TEST test_copy_literal_string(void) {
char *src = "C h a n g e";
int dst_len = 10;
Expand Down Expand Up @@ -48,6 +58,7 @@ TEST length_null_string(void) {

SUITE(suite_string_utils) {
RUN_TEST(test_copy_empty_string);
RUN_TEST(test_copy_null_string);
RUN_TEST(test_copy_literal_string);
RUN_TEST(length_empty_string);
RUN_TEST(length_literal_string);
Expand Down

0 comments on commit edcb70f

Please sign in to comment.