3

It is stated on cppref that

If either dest or src is an invalid or null pointer, the behavior is undefined, even if count is zero

Same is stated for memset and memmove.

My question is: where is it stated in the standard? I need to justify this as an UB to my client, but a link to a cppref article won' t do, I need the reference in the standard.

Public draft of the standard

5
  • You can trust cppreference to be true (even if it is not written in standardese). Also I wouldn't use ::memcpy (memset and memmove) anyway, there is std::copy (or std::bit_cast for type punning) which is more (typesafe) safe to use. And they will not be slow. The C++ standard by the way refers to the "C" standard for memset (which makes sense it is there as a backward compatible option in C++) Commented Jan 24, 2024 at 16:36
  • 1
    Well, it is not I who uses ::memcpy, it is the client, and my job is to detect UBs which I do. Whether they will change it or not to std::cpy is up to them, that' s why I want to point the standard section that says so. I do trust cppref, but I need more than just this as a proof Commented Jan 24, 2024 at 16:41
  • Detecting UB is impossible, because by the time it happend your program is no longer in a valid state. Or do you want to detect invalid input to your own implementation of memcpy? Or Something is not clear to me here. Commented Jan 24, 2024 at 16:44
  • It is indeed possible to detect UB: I work for a company that developped a tool to do it. It's called Trust-in-soft (look it up). My job is to use the tool to audit our clients' code. Commented Jan 24, 2024 at 16:49
  • Oh static analyis, I assumed at runtime. At least it is much more clear now. Commented Jan 24, 2024 at 16:51

1 Answer 1

7

The C++ standard refers the C standard, and these functions are defined in the C standard.

7.24 String handling <string.h>

7.24.1 String function conventions

  1. <...> Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4. <...>

7.1.4 Use of library functions

  1. <...> If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined <...>

C17 ISO/IEC 9899:2018 N2310

Sign up to request clarification or add additional context in comments.

3 Comments

And which part of the C++ standard incorporates the C standard? I don't believe it does, they're independent.
cstring.syn#1 The contents and meaning of the header <cstring> are the same as the C standard library header <string.h>. support.c.headers.general#1 It is possible that C++ source files need to include one of these headers in order to be valid ISO C. Source files that are not intended to also be valid ISO C should not use any of the C headers.
@MarkRansom C++ standard does refer to C standard (and few others). In particular, C++20 refers to ISO/IEC 9899:2018 (C17). I believe the relevant part declaring the C subpart in C++ is isocpp.org/files/papers/N4860.pdf#subsubsection.16.5.1.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.