[PATCH] RISC-V: strcpy size optimized: do not compile unreacheble code

Alexey Lapshin alexey.lapshin@espressif.com
Mon Aug 25 12:20:03 GMT 2025


If misaligned access is allowed on the target machine, there is no need
to compile the byte-to-byte copy loop at the end of the strcpy function.
All bytes are already in place due to the earlier logic.

Signed-off-by: Konstantin Kondrashov <konstantin@espressif.com>
---
 newlib/libc/machine/riscv/rv_string.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/newlib/libc/machine/riscv/rv_string.h b/newlib/libc/machine/riscv/rv_string.h
index dc2a26daf..9f5eb4099 100644
--- a/newlib/libc/machine/riscv/rv_string.h
+++ b/newlib/libc/machine/riscv/rv_string.h
@@ -112,6 +112,8 @@ static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
     }
 #endif /* not PREFER_SIZE_OVER_SPEED */
 
+#if !(!defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) &&
+      !(__riscv_misaligned_slow || __riscv_misaligned_fast))
   char ch;
   do
     {
@@ -122,6 +124,7 @@ static __inline char *__libc_strcpy(char *dst, const char *src, bool ret_start)
     } while (ch);
 
   return ret_start ? dst0 : dst - 1;
+#endif
 }
 
 
-- 
2.43.0



More information about the Newlib mailing list