0

I was using msys2 earlier with gcc 6.4.0 and was working for my project. I upgraded the toolchain and gcc to 9.1.0. Now I am getting this error:

small part of code:

    memset(payload, 0, 8192);
    sprintf(payload, "GET %s%s HTTP/1.1\r\n", dynamic_fota_url, 
    new_firmware_version);
    sprintf(payload, "%sHost: %s\r\n", payload, dynamic_hostname);
    sprintf(payload, "%sPort: 443\r\n", payload);
    sprintf(payload, "%sAccept: */*\r\n\r\n", payload);

and error message is:-

D:/dozee/dozee_compiler/msys32/home/esp-idf/examples/dozee_fw_v62/main/dozee_next.c:2092:12: error: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Werror=restrict]
    sprintf(payload, "%sPort: 443\r\n", payload);
            ^~~~~~~                     ~~~~~~~
D:/dozee/dozee_compiler/msys32/home/esp-idf/examples/dozee_fw_v62/main/dozee_next.c:2093:12: error: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Werror=restrict]
    sprintf(payload, "%sAccept: */*\r\n\r\n", payload);
            ^~~~~~~                           ~~~~~~~
D:/dozee/dozee_compiler/msys32/home/esp-idf/examples/dozee_fw_v62/main/dozee_next.c:2344:15: error: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Werror=restrict]
       sprintf(payload, "%sHost: %s\r\n", payload, dynamic_hostname);
               ^~~~~~~                    ~~~~~~~
D:/dozee/dozee_compiler/msys32/home/esp-idf/examples/dozee_fw_v62/main/dozee_next.c:2345:15: error: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Werror=restrict]
       sprintf(payload, "%sPort: 443\r\n", payload);

How can I fix this. how can I downgrade my gcc to previous version?

1 Answers1

2

error: passing argument 1 to restrict-qualified parameter aliases with argument 3

How to fix this one.

Do NOT alias argument 1 to printf with any other argument.

sprintf(some_buffer, "%s fmt string", some_different_buffer);

Aliasing the buffer is invalid and not allowed to do and results in undefined behavior. The buffers you write to and read from are not allowed to overlap.

Use strcat to append to string. Or calculate offset and append to buffer by shifting buffer starting position you use with sprintf. Prefer using snprintf instead of sprintf. snprintf and sprintf return count of bytes written (excluding zero terminating byte), use that count. A typical usage of appending to buffer with snprintf may look like this:

char payload[200];
int len = snprintf(payload, sizeof(payload), "initialize");
len += snprintf(payload + len, sizeof(payload) - len, "Port: 443\r\n", payload);
len += snprintf(payload + len, sizeof(payload) - len, "Accept: */*\r\n\r\n", payload);
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Is this added in latest gcc compiler for sprintf because when I was compiling with my old gcc it was working fine. BTW thanks for your suggestion and will try this implementation. – Murlidhar Roy Jul 04 '20 at 08:55
  • @MurlidharRoy It's been undefined behavior forever; it's just that gcc only started warning about it in recent versions. – Shawn Jul 04 '20 at 09:01
  • 3
    @MurlidharRoy `it was working fine` is a perfect example of undefined behavior. – KamilCuk Jul 04 '20 at 09:30
  • Why this isgiving this error- `error: '%d' directive writing between 1 and 3 bytes into a region of size 2 [-Werror=format-overflow=] sprintf(gain_str, "%d", live_gain-30); 3824:20: note: directive argument in the range [-30, 225] sprintf(gain_str, "%d", live_gain-30); ^~~~3824:2: note: 'sprintf' output between 2 and 4 bytes into a destination of size 2 sprintf(gain_str, "%d", live_gain-30);` Here char gain_str[2]={0} and unit8_t live_gain=0 initialised – Murlidhar Roy Jul 06 '20 at 10:36
  • Because `%d` `printf` format specifier will write between `1` and `3` bytes into a region of `2` bytes. It's right there in the description. – KamilCuk Jul 06 '20 at 10:39