Page MenuHomeFreeBSD

strdup.c, strndup.c: Prefer memcpy() over bcopy()
ClosedPublic

Authored by zlei on Feb 15 2025, 6:49 PM.
Tags
None
Referenced Files
F132689693: D49026.diff
Sun, Oct 19, 2:01 AM
F132667422: D49026.id151109.diff
Sat, Oct 18, 9:52 PM
Unknown Object (File)
Mon, Sep 29, 2:27 PM
Unknown Object (File)
Sun, Sep 28, 3:18 AM
Unknown Object (File)
Fri, Sep 26, 11:11 PM
Unknown Object (File)
Thu, Sep 25, 7:55 PM
Unknown Object (File)
Mon, Sep 22, 10:33 AM
Unknown Object (File)
Sep 18 2025, 6:54 PM
Subscribers

Details

Summary

The newly allocated memory can not overlap with the string if the string
is properly null-terminated or the maxlen is a valid lengh. Prefer memcpy()
over memmove(), aka bcopy(), for slight performance gain.

No functional change intended.

MFC after: 2 weeks

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

zlei requested review of this revision.Feb 15 2025, 6:49 PM
zlei created this revision.

Out of topic,
@imp
I see @andrew made the change to redirect memcpy() to compiler provided __buildin_memcpy() [1] , but powerpc and riscv still have libkern/bcopy.c compiled into the kernel. I have not tested yet but I guess libkern/bcopy.c is no longer required for powerpc and riscv ?

[1] 849aef496d2a Port the NetBSD KCSAN runtime to FreeBSD

This revision is now accepted and ready to land.Feb 16 2025, 1:48 PM

Out of topic,
@imp
I see @andrew made the change to redirect memcpy() to compiler provided __buildin_memcpy() [1] , but powerpc and riscv still have libkern/bcopy.c compiled into the kernel. I have not tested yet but I guess libkern/bcopy.c is no longer required for powerpc and riscv ?

[1] 849aef496d2a Port the NetBSD KCSAN runtime to FreeBSD

Please ignore the above comment. I thought __buildin_memcpy() always inline and behaves the same with __buildin_memcpy_inline() but that is not true [2].

[2] https://clang.llvm.org/docs/LanguageExtensions.html#memory-builtins