-
Notifications
You must be signed in to change notification settings - Fork 87
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
undefined behaviour in memcpy_s #18
Comments
Cast pointers to be compared to (uintptr_t) as in mem_prim_move "Comparing pointers from two separately allocated objects is forbidden as per 6.5.8 C11 except when using (in)equality." See also intel/safestringlib#18 Closes GH #51
Cast pointers to be compared to (uintptr_t) as in mem_prim_move "Comparing pointers from two separately allocated objects is forbidden as per 6.5.8 C11 except when using (in)equality." See also intel/safestringlib#18 Closes GH #51
Cast pointers to be compared to (uintptr_t) as in mem_prim_move "Comparing pointers from two separately allocated objects is forbidden as per 6.5.8 C11 except when using (in)equality." See also intel/safestringlib#18 Closes GH #51
Thinking about it I came to the conclusion that you are also handling the problem badly. Using memcpy with overlapping ranges is UB. But that means that you can define the behaviour to make it perfectly valid. Thus, instead of bailing out on an overlap, print a warning and just call memmove. |
@kloetzl - thanks for bringing this up. Indeed the use of uintptr would be better. |
That is true for memcpy, but not for memcpy_s. In the case of overlapping memory regions the latter nulls the memory as per the spec; which I think is an error. It could simply forward to memmove. However, as you just want to implement Annex K, your behavior is correct ( |
Sorry but I am not sure that I understand the statement: The library allows a runtime time setting to set the constraint handler - if that is not set (== NULL) Is this to what you are referring? I don't quite understand "UB" though - can you explain? |
That should have been “except”, my bad.
The following comparison is undefined. safestringlib/safeclib/memcpy_s.c Lines 148 to 149 in 38c0fa9
|
Thanks. I'll submit a fix this week for this one, and will look for other instances (unless you happen to know of any). |
Hi,
In
memcpy_s
there is a guard in place to prevent copying overlapping ranges (which is UB). However, the way the check is implemented it will trigger UB in all other cases. Comparing pointers from two separately allocated objects is forbidden as per 6.5.8 C11 except when using (in)equality. I suggest using a cast touintptr_t
as seen in mem_prim_move.Best,
Fabian
/edit: Accidentally wrote
memset_s
at first.The text was updated successfully, but these errors were encountered: