Skip to content

Instantly share code, notes, and snippets.

@the6p4c
Created September 27, 2019 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the6p4c/72e69eda653d4a75c86b9939710dbd5b to your computer and use it in GitHub Desktop.
Save the6p4c/72e69eda653d4a75c86b9939710dbd5b to your computer and use it in GitHub Desktop.
// Build with gcc -o bug bug.c
//
// Prints non-NULL address of y when executed outside of valgrind, and a NULL
// (rendered as "(nil)" by printf) when executed inside valgrind.
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int shmid = shmget(IPC_PRIVATE, 1024 * 1024 * 2, IPC_CREAT | 0666);
if (shmid < 0) {
fprintf(stderr, "Failed to create SHM segment\n");
return 1;
}
char *x = shmat(shmid, NULL, 0);
if (x == (void *) -1) {
fprintf(stderr, "Failed to attach to SHM segment\n");
return 1;
}
char *y = malloc(1);
printf("y = %p\n", y);
free(y);
shmdt(x);
shmctl(shmid, IPC_RMID, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment