Home > Enterprise >  builtin_memcpy warning on linux but not on windows
builtin_memcpy warning on linux but not on windows

Time:12-22

While trying to copy the string larger than the "string" variable, I know the reason for getting this warning, it is because I am trying to fit a 21-byte string into a 6-byte region. But why I am confused is why I am not getting a warning on the windows compiler.

On Windows, I am using Mingw, Visual Studio Code, and it runs the loop but there is no warning of any kind, while on Linux it is showing this warning.

rtos_test.c: In function 'main':
rtos_test.c:18:5: warning: '__builtin_memcpy' writing 21 bytes into a region of size 6 overflows the destination [-Wstringop-overflow=]
   18 |     strcpy(string, "Too long to fit ahan");
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <string.h>

uint8_t test = 0;

char string[] = "Short";

int main()
{
    while (test < 12)
    {
        printf("\nA sample C program\n\n");
        test  ;
    }

    strcpy(string, "Too long to fit ahan");

    return 0;
}

CodePudding user response:

I haven't enough reputation point to comment to your post.

I think in Linux gcc -Wall flag is enabled, you can try add -Wall flag to your IDE on Windows

additional, I checked some compiler I saw that

char string[] = "Short"; only allocate for string with size is 6 your code use string is incorrectly, if you try to use more than allocated space the program may be crashed, you can verified this via asm code on Windows

└─[0] <> gcc test.c -S
test.c: In function ‘main’:
test.c:18:5: warning: ‘__builtin_memcpy’ writing 21 bytes into a region of size 6 overflows the destination [-Wstringop-overflow=]
   18 |     strcpy(stringssss, "Too long to fit ahan");
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌─[longkl@VN-MF10-NC1011M] - [~/tmp] - [2021-12-22 07:00:36]
└─[0] <> grep stringsss test.s 
        .globl  stringssss
        .type   stringssss, @object
        .size   stringssss, 6

CodePudding user response:

This warning on Linux imply that GCC replaced memcpy() to a GCC builtin and that GCC can detect and is configured to detect such error. Which may not be the case on Windows and depending of compiler options, version, mood, etc.

You are also comparing Windows and Linux which are very different platforms, don't expect the same behavior on both. GCC is not very Windows oriented too (MinGW = Minimalist Gnu for Windows). Even between Linux distros, the GCC is different, there is a hugely large amount of variables to consider, especially when optimizations are involved.

To sum up, different environments produce different results, warnings and errors. You can't really do anything against that without fixing your code when you rely on environment specific behavior (often without knowing it), tweaking compiler options or code... Often the answer is to fix your source, the source of your problems ~100% of the time.

As a side note, setting up CI with different environment are a great bug catching system since behavior that looks fine on a system would not on another as in your case where there is memory corruption that would happen on both Linux and Windows.

  • Related