Skip to content

Instantly share code, notes, and snippets.

@davisp
Last active January 12, 2023 16:36
Show Gist options
  • Save davisp/767a5812195c0934a12ec39f6826dc34 to your computer and use it in GitHub Desktop.
Save davisp/767a5812195c0934a12ec39f6826dc34 to your computer and use it in GitHub Desktop.
g++ foo.cc
./a.out
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
int
main(int, char* [])
{
char* buf = nullptr;
int fd = open("blargh.txt", O_WRONLY | O_APPEND | O_CREAT, 755);
ssize_t written = pwrite(fd, 0x0, 2147483647, 0);
std::cerr << "Wrote: " << written << std::endl;
std::cerr << strerror(errno) << std::endl;
}
@janl
Copy link

janl commented Jan 12, 2023

% nano foo.cc
% g++ foo.cc
% ./a.out 
Wrote: -1
Bad address
% cat foo.cc
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <iostream>

int
main(int, char* [])
{
    char* buf = nullptr;
    int fd = open("blargh.txt", O_WRONLY | O_APPEND | O_CREAT, 755);
    ssize_t written = pwrite(fd, 0x0, 2147483647, 0);
    std::cerr << "Wrote: " << written << std::endl;
    std::cerr << strerror(errno) << std::endl;
}
% uname -a
Darwin S2MBP.local 22.2.0 Darwin Kernel Version 22.2.0: Fri Nov 11 02:08:47 PST 2022; root:xnu-8792.61.2~4/RELEASE_X86_64 x86_64

@janl
Copy link

janl commented Jan 12, 2023

 % ls -la blargh.txt 
--wxr----x  1 jan  staff  2147483647 Jan 12 16:22 blargh.txt
% ls -lah blargh.txt 
--wxr----x  1 jan  staff   2.0G Jan 12 16:22 blargh.txt

@leipzig
Copy link

leipzig commented Jan 12, 2023

% g++ --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
% g++ foo.cc
% ./a.out 
Wrote: 0
Undefined error: 0
uname -a
Darwin MacBook-Pro.local 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct  9 20:15:09 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T6000 arm64

% ls -alt blargh.txt 
-rwxr----x  1 leipzig  staff  0 Jan 12 09:31 blargh.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment