- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[libc] mbsrtowcs implementation #145791
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
          
     Closed
      
      
    
      
        
          +354
        
        
          −2
        
        
          
        
      
    
  
  
     Closed
                    [libc] mbsrtowcs implementation #145791
Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      bf0440f
              
                finished most of the implementation except for mbstate checking
              
              
                 73083e4
              
                Merge branch 'main' into mbsrtowcs-implementation
              
              
                 5996aad
              
                Merge branch 'main' into mbsrtowcs-implementation
              
              
                 6f08d75
              
                [libc] mbsrtowcs implementation
              
              
                 7906fc0
              
                fixed behavior when dst is nullptr and removed magic number
              
              
                 1e5ae8d
              
                simplified code and added extra test
              
              
                 59e7344
              
                fixed formatting
              
              
                 18cd136
              
                fixed formatting
              
              
                 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| //===-- Implementation for mbsrtowcs function -------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| #include "src/__support/wchar/mbsrtowcs.h" | ||
| #include "hdr/types/mbstate_t.h" | ||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/error_or.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/wchar/mbrtowc.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
| 
     | 
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
| 
     | 
||
| ErrorOr<size_t> mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src, | ||
| size_t len, mbstate *__restrict ps) { | ||
| size_t i = 0; | ||
| // Converting characters until we reach error or null terminator | ||
| for (; i < len; ++i, ++dst) { | ||
| auto check = mbrtowc(dst, *src, 4, ps); | ||
| // Encoding error/invalid mbstate | ||
| if (!check.has_value()) | ||
| return Error(check.error()); | ||
                
      
                  sribee8 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| // Successfully encoded, check for null terminator | ||
| if (*dst == L'\0') { | ||
| *src = nullptr; | ||
| return i; | ||
| } | ||
| // Set src to point right after the last character converted | ||
| *src = *src + check.value(); | ||
| } | ||
| return i; | ||
| } | ||
| 
     | 
||
| } // namespace internal | ||
| 
     | 
||
| } // namespace LIBC_NAMESPACE_DECL | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //===-- Implementation header for mbsrtowcs function ------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| #ifndef LLVM_LIBC_SRC___SUPPORT_WCHAR_MBSRTOWCS | ||
| #define LLVM_LIBC_SRC___SUPPORT_WCHAR_MBSRTOWCS | ||
| 
     | 
||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/error_or.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
| 
     | 
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace internal { | ||
| 
     | 
||
| ErrorOr<size_t> mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src, | ||
| size_t len, mbstate *__restrict ps); | ||
| 
     | 
||
| } // namespace internal | ||
| 
     | 
||
| } // namespace LIBC_NAMESPACE_DECL | ||
| 
     | 
||
| #endif // LLVM_LIBC_SRC___SUPPORT_WCHAR_MBSRTOWCS | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //===-- Implementation of mbsrtowcs ---------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| #include "src/wchar/mbsrtowcs.h" | ||
| 
     | 
||
| #include "hdr/types/mbstate_t.h" | ||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/libc_errno.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/macros/null_check.h" | ||
| #include "src/__support/wchar/mbsrtowcs.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
| 
     | 
||
| namespace LIBC_NAMESPACE_DECL { | ||
| 
     | 
||
| LLVM_LIBC_FUNCTION(size_t, mbsrtowcs, | ||
| (wchar_t *__restrict dst, const char **__restrict src, | ||
| size_t len, mbstate_t *__restrict ps)) { | ||
| LIBC_CRASH_ON_NULLPTR(src); | ||
| static internal::mbstate internal_mbstate; | ||
| wchar_t temp[len]; | ||
                
      
                  sribee8 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| auto ret = internal::mbsrtowcs( | ||
| dst == nullptr ? temp : dst, src, len, | ||
| ps == nullptr ? &internal_mbstate | ||
| : reinterpret_cast<internal::mbstate *>(ps)); | ||
                
      
                  sribee8 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if (!ret.has_value()) { | ||
| // Encoding failure | ||
| libc_errno = ret.error(); | ||
| return -1; | ||
| } | ||
| return ret.value(); | ||
| } | ||
| 
     | 
||
| } // namespace LIBC_NAMESPACE_DECL | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- Implementation header for mbsrtowcs -------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| #ifndef LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_H | ||
| #define LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_H | ||
| 
     | 
||
| #include "hdr/types/mbstate_t.h" | ||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/macros/config.h" | ||
| 
     | 
||
| namespace LIBC_NAMESPACE_DECL { | ||
| 
     | 
||
| size_t mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src, | ||
| size_t len, mbstate_t *__restrict ps); | ||
| 
     | 
||
| } // namespace LIBC_NAMESPACE_DECL | ||
| 
     | 
||
| #endif // LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_H | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| //===-- Unittests for mbsrtowcs -------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/libc_errno.h" | ||
| #include "src/__support/wchar/mbstate.h" | ||
| #include "src/string/memset.h" | ||
| #include "src/wchar/mbsrtowcs.h" | ||
| #include "test/UnitTest/ErrnoCheckingTest.h" | ||
| #include "test/UnitTest/Test.h" | ||
| 
     | 
||
| using LlvmLibcMBSRToWCSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, OneByteOneCharacter) { | ||
| mbstate_t *mb; | ||
| LIBC_NAMESPACE::memset(&mb, 0, sizeof(mbstate_t)); | ||
| const char *ch = "A"; | ||
| wchar_t dest[2]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &ch, 2, mb); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_TRUE(dest[0] == L'A'); | ||
| ASSERT_TRUE(dest[1] == L'\0'); | ||
| // Should not count null terminator in number | ||
| ASSERT_EQ(static_cast<int>(n), 1); | ||
| // Should set ch to nullptr after reading null terminator | ||
| ASSERT_EQ(ch, nullptr); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, MultiByteOneCharacter) { | ||
                
      
                  sribee8 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| const char *src = "\xf0\x9f\x98\xb9"; // laughing cat emoji 😹 | ||
| wchar_t dest[2]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 2, nullptr); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_EQ(static_cast<int>(dest[0]), 128569); | ||
| ASSERT_TRUE(dest[1] == L'\0'); | ||
| // Should not count null terminator in number | ||
| ASSERT_EQ(static_cast<int>(n), 1); | ||
| // Should set ch to nullptr after reading null terminator | ||
| ASSERT_EQ(src, nullptr); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, MultiByteTwoCharacters) { | ||
| // Two laughing cat emojis "😹😹" | ||
| const char *src = "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; | ||
| wchar_t dest[3]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, nullptr); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_EQ(static_cast<int>(dest[0]), 128569); | ||
| ASSERT_EQ(static_cast<int>(dest[1]), 128569); | ||
| ASSERT_TRUE(dest[2] == L'\0'); | ||
| // Should not count null terminator in number | ||
| ASSERT_EQ(static_cast<int>(n), 2); | ||
| // Should set ch to nullptr after reading null terminator | ||
| ASSERT_EQ(src, nullptr); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, ReadLessThanStringLength) { | ||
| // Four laughing cat emojis "😹😹😹😹" | ||
| const char *src = | ||
| "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; | ||
| const char *check = src; | ||
| wchar_t dest[3]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, nullptr); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| // Should have read 3 emojis | ||
| ASSERT_EQ(static_cast<int>(n), 3); | ||
| ASSERT_EQ(static_cast<int>(dest[0]), 128569); | ||
| ASSERT_EQ(static_cast<int>(dest[1]), 128569); | ||
| ASSERT_EQ(static_cast<int>(dest[2]), 128569); | ||
| // src should now point to the 4th cat emoji aka 13th byte | ||
| ASSERT_EQ((check + 12), src); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, InvalidFirstByte) { | ||
| // 0x80 is invalid first byte of mb character | ||
| const char *src = | ||
| "\x80\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; | ||
| wchar_t dest[3]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, nullptr); | ||
| // Should return error and set errno | ||
| ASSERT_EQ(static_cast<int>(n), -1); | ||
| ASSERT_ERRNO_EQ(EILSEQ); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, InvalidMiddleByte) { | ||
| // The 7th byte is invalid for a 4 byte character | ||
| const char *src = | ||
| "\xf0\x9f\x98\xb9\xf0\x9f\xf0\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; | ||
| wchar_t dest[3]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 5, nullptr); | ||
| // Should return error and set errno | ||
| ASSERT_EQ(static_cast<int>(n), -1); | ||
| ASSERT_ERRNO_EQ(EILSEQ); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, NullDestination) { | ||
| // Four laughing cat emojis "😹😹😹😹" | ||
| const char *src = | ||
| "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(nullptr, &src, 5, nullptr); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| // Null destination should still return correct number of read chars | ||
| ASSERT_EQ(static_cast<int>(n), 4); | ||
| } | ||
| 
     | 
||
| TEST_F(LlvmLibcMBSRToWCSTest, InvalidMBState) { | ||
| mbstate_t *mb; | ||
| LIBC_NAMESPACE::internal::mbstate inv; | ||
| inv.total_bytes = 6; | ||
| mb = reinterpret_cast<mbstate_t *>(&inv); | ||
| // Four laughing cat emojis "😹😹😹😹" | ||
| const char *src = | ||
| "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; | ||
| wchar_t dest[3]; | ||
| size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, mb); | ||
| // Should fail from invalid mbstate | ||
| ASSERT_EQ(static_cast<int>(n), -1); | ||
| ASSERT_ERRNO_EQ(EINVAL); | ||
| } | ||
| 
     | 
||
| #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) | ||
| TEST_F(LlvmLibcMBSRToWCSTest, NullSource) { | ||
| // Passing in a nullptr source should crash the program | ||
| EXPECT_DEATH([] { LIBC_NAMESPACE::mbsrtowcs(nullptr, nullptr, 1, nullptr); }, | ||
| WITH_SIGNAL(-1)); | ||
| } | ||
| #endif // LIBC_HAS_ADDRESS_SANITIZER | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.