Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active June 23, 2024 21:31
Show Gist options
  • Save matu3ba/1b1edf44fbe50ebd258eda29907beac1 to your computer and use it in GitHub Desktop.
Save matu3ba/1b1edf44fbe50ebd258eda29907beac1 to your computer and use it in GitHub Desktop.
clang c89 ptr to int -Weverything
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void ptrtointtoptr(void);
static void memset_16aligned(void * ptr, char byte, size_t size_bytes, uint16_t alignment) {
assert((size_bytes & (alignment-1)) == 0);
assert(((uintptr_t)ptr & (alignment-1)) == 0);
memset(ptr, byte, size_bytes);
}
void ptrtointtoptr(void) {
const uint16_t alignment = 16;
const uint16_t align_min_1 = alignment - 1;
void * mem = malloc(1024+align_min_1);
void *ptr = (void *)((((uint64_t)mem)+align_min_1) & ~(uint64_t)align_min_1);
printf("0x%08" PRIXPTR ", 0x%08" PRIXPTR "\n", (uintptr_t)mem, (uintptr_t)ptr);
memset_16aligned(ptr, 0, 1024, alignment);
free(mem);
}
int main(void) { ptrtointtoptr(); }
misterspoon@pc ~/d/t/tryc> clang -std=c89 -Weverything ptrtoint_inttoptr.c && ./a.out
0x623AA76542A0, 0x623AA76542A0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment