1

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;
}

ZBT248
  • 61
  • 9
  • 2
    Have you enabled the warnings on Windows? – kiner_shah Dec 22 '21 at 07:15
  • 1
    `/Wall /permissive-` – Ted Lyngmo Dec 22 '21 at 07:31
  • 1
    Maybe not turning on the right warning, maybe using an older compiler version that doesn't have that check, sometimes warnings only show up with optimization turned on... – Shawn Dec 22 '21 at 07:36
  • How can I turn on the warning in visual studio code? I know these flags are somehow related to the makefile, but any help would be nice. – ZBT248 Dec 22 '21 at 07:56
  • Is [this question](https://stackoverflow.com/q/58900260/4142924) any help? Or [this MS page](https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170&viewFallbackFrom=vs-2019). – Weather Vane Dec 22 '21 at 08:09
  • @WeatherVane I do not think so. – ZBT248 Dec 22 '21 at 08:15
  • You mean Microsofts's own discussion [To set the compiler options in the Visual Studio development environment](https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170&viewFallbackFrom=vs-2019#to-set-the-compiler-options-in-the-visual-studio-development-environment) is no help? Why? – Weather Vane Dec 22 '21 at 08:40
  • @WeatherVane Because OP is using gcc/mingw, not Microsoft's compiler? – Shawn Dec 22 '21 at 09:05
  • 1
    @ZBT248 Different compilers, and even different versions of the same compiler, differ widely in their dedication to generating warnings like these. There are also wide variations in whether such warnings are emitted by default, or must be explicitly requested with various command-line options. It's extremely unlikely that you're using the identical version of the identical compiler under both Linux and Windows, so it's not surprising that you're getting different warnings. – Steve Summit Dec 22 '21 at 12:13

2 Answers2

0

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
long.kl
  • 670
  • 4
  • 12
  • As I have written above, I know the problem in the code, but I want my window compiler to give me the warning too like Linux is doing, problem that I need to solve is the warning problem and not the code logic problem. – ZBT248 Dec 22 '21 at 12:17
  • @ZBT248 Yes, let's try to add -Wall flag to your IDE – long.kl Dec 22 '21 at 15:14
0

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.

Devilish Spirits
  • 439
  • 4
  • 18