Skip to content

Instantly share code, notes, and snippets.

@alexnask
Created June 8, 2012 16:53
Show Gist options
  • Select an option

  • Save alexnask/2896841 to your computer and use it in GitHub Desktop.

Select an option

Save alexnask/2896841 to your computer and use it in GitHub Desktop.
C version of strange ooc generics bug
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
typedef struct {
int min;
int max;
} Range;
void slice(uint8_t* genReturn, uint8_t* data, Range range) {
int size = range.max - range.min;
uint8_t* ret = ((uint8_t*) (malloc(size * 4)));
{
int i;
for (i = 0; i < size; i++) {
memcpy(&(ret[i * 4]), &(data[(range.min + i) * 4]), 4);
}
}
if (genReturn) {
genReturn = ret;
}
return;
}
int* list;
int* data;
void main() {
list = ((int*) ((int[]) { 0, 1, 2, 3, 4 }));
if(!data) printf("OK D:\n"); // This is printed, this is the problem <--- genReturn != NULL is false so genReturn is never set to ret ;)
slice((uint8_t*) data, (uint8_t*) list, (Range) { 0, 3 });
if (!data) {
printf("Dafuq!\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment