Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessarily calling slower functions #17

Open
N-R-K opened this issue Jul 31, 2023 · 1 comment
Open

Avoid unnecessarily calling slower functions #17

N-R-K opened this issue Jul 31, 2023 · 1 comment
Labels
good first issue Good for newcomers

Comments

@N-R-K
Copy link

N-R-K commented Jul 31, 2023

For example, in strcpy (ignoring the UB overlapping check):

size_t __n = strlen(__s) + 1;
/* trap if pointers are overlapping but not if dst == src.
* gcc seems to like to generate code that relies on dst == src */
if ((__d < __s && __d + __n > __s) ||
(__s < __d && __s + __n > __d))
__builtin_trap();
size_t __b = __bos(__d, 0);
if (__n > __b)
__builtin_trap();
return __orig_strcpy(__d, __s);

By the time, __orig_strcpy is called, the length has already been computed and has been established to be in bound. There's no need to pay for the nul-byte search again inside of __orig_strcpy when the faster memcpy can be called instead.

Some of the other str* functions suffer from the same issue.

@N-R-K N-R-K changed the title Avoid unnecessarily calling slow functions Avoid unnecessarily calling slower functions Jul 31, 2023
@jvoisin
Copy link
Owner

jvoisin commented Jul 31, 2023

The rationale behind this was that maybe other things were "hooking" strcpy, but you're right, a call to memcpy makes sense.

@jvoisin jvoisin added the good first issue Good for newcomers label Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants