Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix sentry__path_from_str[_n] on windows
Browse files Browse the repository at this point in the history
supervacuus committed Apr 24, 2023
1 parent 2b3a5f6 commit 16c8c30
Showing 4 changed files with 38 additions and 21 deletions.
36 changes: 18 additions & 18 deletions src/path/sentry_path_windows.c
Original file line number Diff line number Diff line change
@@ -201,42 +201,42 @@ sentry__path_join_wstr(const sentry_path_t *base, const wchar_t *other)
}

sentry_path_t *
sentry__path_from_str(const char *s)
sentry__path_from_str_n(const char *s, size_t s_len)
{
if (!s) {
return NULL;
}
size_t len = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0);
sentry_path_t *rv = SENTRY_MAKE(sentry_path_t);
if (!rv) {
return NULL;
}
rv->path = sentry_malloc(sizeof(wchar_t) * len);
size_t src_size = sizeof(char) * s_len;
size_t dst_size = sizeof(wchar_t) * (s_len + 1);
rv->path = sentry_malloc(dst_size);
if (!rv->path) {
sentry_free(rv);
return NULL;
goto error;
}
MultiByteToWideChar(CP_ACP, 0, s, -1, rv->path, (int)len);
if (0
== MultiByteToWideChar(
CP_ACP, 0, s, (int)src_size, rv->path, (int)s_len)) {
goto error;
}
rv->path[s_len] = 0;
return rv;

error:
sentry_free(rv);
return NULL;
}

sentry_path_t *
sentry__path_from_str_n(const char *s, size_t s_len)
sentry__path_from_str(const char *s)
{
if (!s) {
return NULL;
}
sentry_path_t *rv = SENTRY_MAKE(sentry_path_t);
if (!rv) {
return NULL;
}
rv->path = sentry_malloc(sizeof(wchar_t) * (s_len + 1));
if (!rv->path) {
sentry_free(rv);
return NULL;
}
MultiByteToWideChar(CP_ACP, 0, s, -1, rv->path, (int)s_len + 1);
return rv;

return sentry__path_from_str_n(s, strlen(s));
}

sentry_path_t *
3 changes: 0 additions & 3 deletions src/sentry_path.h
Original file line number Diff line number Diff line change
@@ -54,9 +54,6 @@ sentry_path_t *sentry__path_dir(const sentry_path_t *path);
*/
sentry_path_t *sentry__path_from_str(const char *s);
sentry_path_t *sentry__path_from_str_n(const char *s, size_t s_len);
#define CALL_SENTRY__PATH_FROM_STR(STR) sentry__path_from_str(STR)
#define CALL_SENTRY__PATH_FROM_STR_N(STR) \
sentry__path_from_str_n(STR, STR##_len)

/**
* Create a new path from the given string.
7 changes: 7 additions & 0 deletions tests/unit/sentry_testsupport.h
Original file line number Diff line number Diff line change
@@ -23,6 +23,13 @@
TEST_MSG("Received: %s", Val); \
} while (0)

#define TEST_CHECK_WSTRING_EQUAL(Val, ReferenceVal) \
do { \
TEST_CHECK(wcscmp(Val, ReferenceVal) == 0); \
TEST_MSG("Expected: %s", ReferenceVal); \
TEST_MSG("Received: %s", Val); \
} while (0)

#define TEST_CHECK_JSON_VALUE(Val, ReferenceJson) \
do { \
char *json = sentry_value_to_json(Val); \
13 changes: 13 additions & 0 deletions tests/unit/test_path.c
Original file line number Diff line number Diff line change
@@ -65,6 +65,19 @@ SENTRY_TEST(path_from_str_null)
TEST_CHECK(NULL == sentry__path_from_str_n(NULL, 10));
}

SENTRY_TEST(path_from_str_n_wo_null_termination)
{
// provide non-null-terminated path string with buffer character at the end.
char path_str[] = { 't', 'e', 's', 't', 'X' };
sentry_path_t *test_path = sentry__path_from_str_n(path_str, 4);
#ifdef SENTRY_PLATFORM_WINDOWS
TEST_CHECK_WSTRING_EQUAL(test_path->path, L"test");
#else
TEST_CHECK_STRING_EQUAL(test_path->path, "test");
#endif
sentry__path_free(test_path);
}

SENTRY_TEST(path_joining_windows)
{
#ifndef SENTRY_PLATFORM_WINDOWS

0 comments on commit 16c8c30

Please sign in to comment.