Сообщение [UPD] забавный крэш от 30.09.2021 20:15
Изменено 30.09.2021 21:05 ononim
#include <string>
#include <iostream>
bool foobar(const std::string &s)
{
std::string sx = s;
std::cout << sx << std::endl;
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
.. а если поменять bool foobar на void foobar, или добавить return false, то не падает. Ну подумаешь забыл вернуть bool/home/user/test$ g++ -O2 -std=c++11 ./test.cpp -o ./test && ./test
./test.cpp: In function 'bool foobar(const string&)':
./test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]
8 | }
| ^
./test
Segmentation fault (core dumped)
С GCC 7.5.0 не падает кстати.
[UPD]
std::string как выяснилось непричем, чисто сишный аналог так же падает, будучи собранным g++:
#include <stdio.h>
bool foobar(const char *s)
{
printf("foobar(%s)\n", s);
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
/home/user/test$ g++ -O2 ./test.c -o ./test && ./test
./test.c: In function 'int foobar(const char*)':
./test.c:6:1: warning: no return statement in function returning non-void [-Wreturn-type]
6 | }
| ^
foobar(./test)
Segmentation fault (core dumped)
а вот gcc -O2 ./test.c -o ./test && ./test — бодрячком
Ну я понимаю конечно что undefined behaviour, но зачем прям сразу начисто ногу отстреливать то
#include <string>
#include <iostream>
bool foobar(const std::string &s)
{
std::string sx = s;
std::cout << sx << std::endl;
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
.. а если поменять bool foobar на void foobar, или добавить return false, то не падает. Ну подумаешь забыл вернуть bool/home/user/test$ g++ -O2 -std=c++11 ./test.cpp -o ./test && ./test
./test.cpp: In function 'bool foobar(const string&)':
./test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]
8 | }
| ^
./test
Segmentation fault (core dumped)
С GCC 7.5.0 не падает кстати.
[UPD]
std::string как выяснилось непричем, чисто сишный аналог так же падает, будучи собранным g++:
#include <stdio.h>
bool foobar(const char *s)
{
printf("foobar(%s)\n", s);
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
/home/user/test$ g++ -O2 ./test.c -o ./test && ./test
./test.c: In function 'int foobar(const char*)':
./test.c:6:1: warning: no return statement in function returning non-void [-Wreturn-type]
6 | }
| ^
foobar(./test)
Segmentation fault (core dumped)
а вот gcc -O2 ./test.c -o ./test && ./test — бодрячком
как выяснилось компилятор просто не генерит инструкцию ретурна
0000000000001150 <_Z6foobarPKc>:
1150: 48 89 fe mov rsi,rdi
1153: 48 83 ec 08 sub rsp,0x8
1157: 48 8d 3d a6 0e 00 00 lea rdi,[rip+0xea6] # 2004 <_IO_stdin_used+0x4>
115e: 31 c0 xor eax,eax
1160: e8 cb fe ff ff call 1030 <printf@plt>
1165: 66 2e 0f 1f 84 00 00 00 00 00 cs nop WORD PTR [rax+rax*1+0x0]
116f: 90 nop
0000000000001170 <__libc_csu_init>:
1170: f3 0f 1e fa endbr64
1174: 41 57 push r15
...
...ну я понимаю конечно что undefined behaviour, но зачем прям сразу начисто ногу отстреливать то