Skip to content

minor change: Utilize strlcpy and strlcat instead of strcpy and strcat#6963

Open
gemmaro wants to merge 3 commits into
mruby:masterfrom
gemmaro:re/strlcpy
Open

minor change: Utilize strlcpy and strlcat instead of strcpy and strcat#6963
gemmaro wants to merge 3 commits into
mruby:masterfrom
gemmaro:re/strlcpy

Conversation

@gemmaro

@gemmaro gemmaro commented Jul 23, 2026

Copy link
Copy Markdown

This changeset replaces strcpy with strlcpy and strcat with strlcat.

These new functions is size-bounded and eliminate warnings like this (on OpenBSD, for instance):

ld: warning: diagnostic.c:85 (mrbgems/mruby-compiler/src/diagnostic.c:85)(diagnostic.o:(mrc_diagnostic_list_append) in archive /home/gemmaro/src/mruby/build/host/lib/libmruby.a): warning: strcpy() is almost always misused, please use strlcpy()
ld: warning: cmdmisc.c:268 (mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c:268)(/home/gemmaro/src/mruby/build/host/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.o:(replace_ext)): warning: strcat() is almost always misused, please use strlcat()

Related issues: #383 #4675

Thank you,

@gemmaro
gemmaro requested a review from matz as a code owner July 23, 2026 04:45
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@matz

matz commented Jul 23, 2026

Copy link
Copy Markdown
Member

Thank you for the patch, and for caring about bounded string operations. The intent is good, but I cannot merge this as it stands, because strlcpy and strlcat are not portable enough for mruby's targets.

These two functions are BSD extensions, not part of standard C. They exist on the BSDs and macOS, on musl, and on glibc only since 2.38 (2023). They are absent on older glibc (for example Ubuntu 22.04 ships glibc 2.35), on MSVC, usually on MinGW's msvcrt, and on many embedded libcs. mruby explicitly supports embedded targets, so we cannot assume the C library provides them.

This is not only a link-time issue. mruby compiles C with -Werror-implicit-function-declaration, so on any platform whose <string.h> does not declare these functions, the build fails at compile time rather than warning. Our CI already covers such platforms (Ubuntu 22.04 gcc/clang, Windows MSVC), and this change touches core files that every build compiles, including embedded ones: src/numeric.c (mrb_float_to_str) and mrbgems/mruby-compiler/src/diagnostic.c.

To take this improvement, mruby first needs its own portable strlcpy/strlcat: a fallback implementation used when the C library does not provide them, selected by feature detection. Once that is in place, the call sites here can use them safely. Alternatively, the bounded copies can be written with snprintf, which is portable.

I would be happy to take a revised version along those lines. Thanks again.

@gemmaro

gemmaro commented Jul 23, 2026

Copy link
Copy Markdown
Author

Thank you for the review!
I added macros MRB_STRLCPY and MRB_STRLCAT to provide fallbacks via snprintf.

Reference links for the conditionals (brief notes in comments):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants