Skip to content

Commit 04a67fd

Browse files
committed
added death test
1 parent 9049c06 commit 04a67fd

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

libc/src/__support/wchar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ add_object_library(
4949
libc.hdr.types.wchar_t
5050
libc.src.__support.error_or
5151
libc.src.__support.common
52+
libc.src.__support.libc_assert
5253
.character_converter
5354
.mbstate
5455
.wcrtomb

libc/src/__support/wchar/wcsrtombs.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/wchar/wcsrtombs.h"
10-
#include "src/__support/error_or.h"
11-
#include "src/__support/wchar/character_converter.h"
12-
#include "src/__support/wchar/mbstate.h"
13-
#include "src/__support/wchar/wcrtomb.h"
14-
1510
#include "hdr/types/char32_t.h"
1611
#include "hdr/types/size_t.h"
1712
#include "hdr/types/wchar_t.h"
1813
#include "src/__support/common.h"
14+
#include "src/__support/error_or.h"
1915
#include "src/__support/libc_assert.h"
16+
#include "src/__support/wchar/character_converter.h"
17+
#include "src/__support/wchar/mbstate.h"
18+
#include "src/__support/wchar/wcrtomb.h"
2019

2120
namespace LIBC_NAMESPACE_DECL {
2221
namespace internal {

libc/src/wchar/wcsrtombs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LLVM_LIBC_FUNCTION(size_t, wcsrtombs,
3232
: reinterpret_cast<internal::mbstate *>(ps));
3333

3434
if (!result.has_value()) {
35-
libc_errno = EILSEQ;
35+
libc_errno = result.error();
3636
return -1;
3737
}
3838

libc/test/src/wchar/wcsrtombs_test.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
#include "hdr/types/mbstate_t.h"
1010
#include "hdr/types/wchar_t.h"
11+
#include "src/__support/wchar/mbstate.h"
1112
#include "src/string/memset.h"
1213
#include "src/wchar/wcsrtombs.h"
1314
#include "test/UnitTest/ErrnoCheckingTest.h"
1415
#include "test/UnitTest/Test.h"
15-
#include "src/__support/wchar/mbstate.h"
1616

1717
using LlvmLibcWCSRToMBSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
1818

19-
TEST(LlvmLibcWCSRToMBSTest, SingleCharacterOneByte) {
19+
TEST_F(LlvmLibcWCSRToMBSTest, SingleCharacterOneByte) {
2020
mbstate_t state;
2121
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
2222
const wchar_t *wcs = L"U";
@@ -28,7 +28,7 @@ TEST(LlvmLibcWCSRToMBSTest, SingleCharacterOneByte) {
2828
ASSERT_EQ(wcs, nullptr);
2929
}
3030

31-
TEST(LlvmLibcWCSRToMBSTest, MultipleCompleteConversions) {
31+
TEST_F(LlvmLibcWCSRToMBSTest, MultipleCompleteConversions) {
3232
mbstate_t state;
3333
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
3434

@@ -71,7 +71,7 @@ TEST(LlvmLibcWCSRToMBSTest, MultipleCompleteConversions) {
7171
ASSERT_EQ(mbs[6], '\x01'); // should not write beyond null terminator
7272
}
7373

74-
TEST(LlvmLibcWCSRToMBSTest, MultiplePartialConversions) {
74+
TEST_F(LlvmLibcWCSRToMBSTest, MultiplePartialConversions) {
7575
mbstate_t state;
7676
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
7777

@@ -115,7 +115,7 @@ TEST(LlvmLibcWCSRToMBSTest, MultiplePartialConversions) {
115115
ASSERT_EQ(mbs[6], '\x01');
116116
}
117117

118-
TEST(LlvmLibcWCSRToMBSTest, NullDestination) {
118+
TEST_F(LlvmLibcWCSRToMBSTest, NullDestination) {
119119
mbstate_t state;
120120
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
121121

@@ -130,7 +130,7 @@ TEST(LlvmLibcWCSRToMBSTest, NullDestination) {
130130
ASSERT_EQ(wcs, nullptr);
131131
}
132132

133-
TEST(LlvmLibcWCSRToMBSTest, NullState) {
133+
TEST_F(LlvmLibcWCSRToMBSTest, NullState) {
134134
// same as MultiplePartialConversions test except without an explicit
135135
// mbstate_t
136136

@@ -172,7 +172,7 @@ TEST(LlvmLibcWCSRToMBSTest, NullState) {
172172
ASSERT_EQ(mbs[6], '\x01');
173173
}
174174

175-
TEST(LlvmLibcWCSRToMBSTest, InvalidWchar) {
175+
TEST_F(LlvmLibcWCSRToMBSTest, InvalidWchar) {
176176
mbstate_t state;
177177
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
178178

@@ -189,12 +189,25 @@ TEST(LlvmLibcWCSRToMBSTest, InvalidWchar) {
189189
ASSERT_ERRNO_EQ(EILSEQ);
190190
}
191191

192-
TEST(LlvmLibcWCSRToMBSTest, InvalidState) {
192+
TEST_F(LlvmLibcWCSRToMBSTest, InvalidState) {
193193
LIBC_NAMESPACE::internal::mbstate state{0, 0, 9}; // 9 total bytes is invalid
194194
const wchar_t *wcs = L"\xFF\xAC15";
195195
char mbs[5];
196196
// convert the valid wchar
197-
size_t count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 5, reinterpret_cast<mbstate_t*>(&state));
197+
size_t count = LIBC_NAMESPACE::wcsrtombs(
198+
mbs, &wcs, 5, reinterpret_cast<mbstate_t *>(&state));
198199
ASSERT_EQ(count, static_cast<size_t>(-1));
199200
ASSERT_ERRNO_EQ(EINVAL);
200201
}
202+
203+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
204+
TEST_F(LlvmLibcWCSRToMBSTest, NullSrc) {
205+
// Passing in a nullptr should crash the program.
206+
char mbs[] = {0, 0};
207+
EXPECT_DEATH(
208+
[&mbs] {
209+
LIBC_NAMESPACE::wcsrtombs(mbs, nullptr, 2, nullptr);
210+
},
211+
WITH_SIGNAL(-1));
212+
}
213+
#endif // LIBC_HAS_ADDRESS_SANITIZER

0 commit comments

Comments
 (0)