Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is gcc -Wstringop-overflow complaining about here?

The following code (reduced from a larger, more sensible sample):

#include <vector>

void shrink(std::vector<int>& v) {
    while (v.size() > 0) {
        v.resize(v.size() - 1);
    }
}

Leads gcc 7.3 to emit this warning (godbolt):

In function 'void shrink(std::vector<int>&)':
cc1plus: warning: 'void* __builtin_memset(void*, int, long unsigned int)':
specified size 18446744073709551612 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]

I have been staring at this code for close to an hour with a colleague, and it just seems correct to me; what is gcc complaining about?

like image 271
Matthieu M. Avatar asked Nov 27 '25 00:11

Matthieu M.


1 Answers

it just seems correct to me

The example is correct.

what is gcc complaining about?

This is a compiler bug. Here is the bugzilla. The bug appears to be fixed in GCC 8.

like image 74
eerorika Avatar answered Nov 29 '25 14:11

eerorika