Ticket #17751: test.cpp

File test.cpp, 1.1 KB (added by trungnt2910, 2 years ago)

Minimal C++ program for testing this bug

Line 
1#include <iostream>
2
3#include <fcntl.h>
4#include <sys/mman.h>
5
6int main()
7{
8 int fds[2];
9 int flags;
10
11 if (pipe(fds) != 0)
12 {
13 printf("pipe failed: errno is %d (%s)\n", errno, strerror(errno));
14 return false;
15 }
16
17 flags = fcntl(fds[0], F_GETFL, 0);
18 fcntl(fds[0], F_SETFL, flags | O_NONBLOCK);
19
20 flags = fcntl(fds[1], F_GETFL, 0);
21 fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
22
23 const int size = 4096;
24 void* ptr = mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
25 // Crashes
26 // std::cout << "Pointer value is: " << std::flush << *(int*)ptr << std::endl;
27 // Still returns 1, expected -1
28 std::cout << write(fds[1], ptr, 1) << std::endl;
29 // false, expected true
30 std::cout << std::boolalpha << (errno == EFAULT) << std::noboolalpha << std::endl;
31 // -1 as expected
32 std::cout << write(fds[1], (void*)0xDEADBEEF, 1) << std::endl;
33 // true as expected.
34 std::cout << std::boolalpha << (errno == EFAULT) << std::noboolalpha << std::endl;
35 return 0;
36}