Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bsg_strncpy should terminate dst even if src is NULL #1637

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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