Compiler Warning (Level 1) C4789

destination of memory copy is too small

Data whose length is known at compile time was copied into a data block whose size is also known at compile time and whose size is too small for the data, and where the copy was done by the intrinsic form of one of the following CRT functions:

Compiling with /Oi- or /Od will resolve this warning

Example

The following sample generates C4789.

// C4789.cpp
// compile with: /Oi /W1 /c
#include <string.h>
#include <stdio.h>

int main() 
{
    char a[20];
    strcpy(a, "0000000000000000000000000\n");   // C4789

    char buf2[20];
    memset(buf2, 'a', 21);   // C4789

    char c;
    wchar_t w = 0;
    memcpy(&c, &w, sizeof(wchar_t));
}

The following sample generates C4789.

// C4789b.cpp
// compile with: /W1 /O2
// processor: x86
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() 
{
    // /O2 collapses this loop on x86 into a memset intrinsic and warn
    // on Itanium, only explicit memset calll will generate C4789
    char buf[98];

    memset(buf, 0, sizeof(buf)+1);
    for ( int i = 0; i < 99; i++ )
        buf[i] = 'a';   // C4789
    printf_s("%c\n", buf[0]);
}