Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Is strcpy dangerous and what should be used instead?

strcpy(3) can be safe. Some compilers, such as GCC and Clang, use a feature test macro, _FORTIFY_SOURCE, (see feature_test_macros(7) https://man7.org/linux/man-pages/man7/feature_test_macros.7.htm...

posted 1y ago by alx‭

Answer
#1: Initial revision by user avatar alx‭ · 2023-10-16T11:56:53Z (about 1 year ago)
strcpy(3) can be safe.  Some compilers, such as GCC and Clang, use a feature test macro, `_FORTIFY_SOURCE`, (see feature_test_macros(7) <https://man7.org/linux/man-pages/man7/feature_test_macros.7.html>), to ask the compiler to add some checks to make sure that buffer overflow doesn't happen.

If the bug is detected at compile time, it will raise a warning.  If the bug is detected at run time, it will abort(3) the program.

This is the simplest thing a programmer can do to prevent the problems that strcpy(3) can cause.

---

### Truncating

Another way to avoid buffer overflow is truncating the string.  This adds complexity to the code, which can itself cause more bugs.  In general, if you can use `_FORTIFY_SOURCE`, it's preferred.

If you do this, you need to:

-  Specify the limiting size.
-  Check the return value of the functions, to detect truncation.
-  Do something if you detect truncation!

The last step is usually neglected, causing second-order bugs.
Continuing the program with a truncated string can be very dangerous too.

If you really need to do this, there's no standard function in ISO C.  In POSIX, strlcpy(3) and strlcat(3) will be added soon (in Issue 8, POSIX.1-202x).  If you need to chain several such calls, strlcat(3) is hard to use (you need to check the return value after every call); you may want to use stpecpy(3), which you'll need to write yourself (see `man 3 stpecpy` <https://man.archlinux.org/man/stpecpy.3> for a simple implementation).  In case your system doesn't provide strlcpy(3), you may also want to write your own stpecpy(3) implementation.