'Is conditionally not modifying constant data undefined behavior in C?

We have the following function:

void foo(int flag, void *ptr) {
    if (flag)
        strcpy(ptr, "Hello World");
    code_that_does_not_attempt_to_modify_data_pointed_to_by(ptr);
}

Would the following be valid:

const char *string_literal_ptr = "String literals are constant and may not be modified";
foo(0, string_literal_ptr);

We are passing a pointer to constant data to a function that may (but will not because we passed 0 as flag) modify the data pointed to by the pointer. Is this valid, given that at no point the program control reaches the point of modifying the constant data?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source