Skip to content

Conversation

@Sterling-Augustine
Copy link
Contributor

This options sets a compile option when building sources inside the string directory, and this option affects string_utils.h. But string_utils.h is #included from more places than just the string directory (such as from __support/CPP/string.h), leading to both narrow-reads in those cases, but more seriously, ODR violations when the two different string_length implementations are included int he same program.

Having this option at the top level avoids this problem.

This options sets a compile option when building sources inside the string
directory, and this option affects string_utils.h. But string_utils.h
is #included from more places than just the string directory (such as from
__support/CPP/string.h), leading to both narrow-reads in those cases, but
more seriously, ODR violations when the two different string_length
implementations are included int he same program.

Having this option at the top level avoids this problem.
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2025

@llvm/pr-subscribers-libc

Author: None (Sterling-Augustine)

Changes

This options sets a compile option when building sources inside the string directory, and this option affects string_utils.h. But string_utils.h is #included from more places than just the string directory (such as from __support/CPP/string.h), leading to both narrow-reads in those cases, but more seriously, ODR violations when the two different string_length implementations are included int he same program.

Having this option at the top level avoids this problem.


Full diff: https://github.com/llvm/llvm-project/pull/165046.diff

2 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+4)
  • (modified) libc/src/string/CMakeLists.txt (-3)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 4c36ed8620f40..c4e4a9e3465d7 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -81,6 +81,10 @@ function(_get_compile_options_from_config output_var)
     list(APPEND config_options "-DLIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")
   endif()
 
+  if(LIBC_CONF_STRING_UNSAFE_WIDE_READ)
+    list(APPEND config_options "-DLIBC_COPT_STRING_UNSAFE_WIDE_READ")
+  endif()
+
   if(LIBC_TYPES_TIME_T_IS_32_BIT AND LLVM_LIBC_FULL_BUILD)
     list(APPEND config_options "-DLIBC_TYPES_TIME_T_IS_32_BIT")
   endif()
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 83c956429be24..fd1ae8d417f8c 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -1,8 +1,5 @@
 add_subdirectory(memory_utils)
 
-if(LIBC_CONF_STRING_UNSAFE_WIDE_READ)
-  list(APPEND string_config_options "-DLIBC_COPT_STRING_UNSAFE_WIDE_READ")
-endif()
 if(LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING)
   list(APPEND string_config_options "-DLIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING")
 endif()

Comment on lines 3 to 5
if(LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING)
list(APPEND string_config_options "-DLIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING")
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you're here, do you mind also moving this config to top-level and removing string_config_options here? Thanks,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 3 to 5
if(string_config_options)
list(PREPEND string_config_options "COMPILE_OPTIONS")
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Remove this if() ... endif() and ${string_config_options} in add_header_library(string_utils ... ) below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Sterling-Augustine Sterling-Augustine merged commit 7d1538c into llvm:main Oct 27, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants