Weird behavior with C's file IO -



Weird behavior with C's file IO -

i've been writing virtual machine, , i've noticed unusual things have happened, though wrote function ages ago. anyhow, virtual machine reads file this:

0002 000a 0001 0004 0000

however, when don't have whitespace after 0000 or new line... crashes. weird thing, leads me believe it's not file loading, when remove 0000 file, , whitespace... works?! i've tried running through gdb, works -- apparently called heisenbug or something. think it's due way files loaded, can see in this function here on github, or read snippet below.

void load_program(vm *self) { file *file = fopen("testing.ayy", "r"); if (file != null) { if (fseek(file, 0, seek_end) == 0) { long file_size = ftell(file); if (file_size == -1) { perror("could not read filesize\n"); exit(1); } self->source = malloc(sizeof(char) * file_size); if (fseek(file, 0, seek_set) != 0) { perror("could not reset file index\n"); exit(1); } size_t file_length = fread(self->source, sizeof(char), file_size, file); if (file_length == 0) { perror("given file empty\n"); exit(1); } self->source[file_size] = '\0'; } fclose(file); } else { perror("could not read file: \n"); exit(1); } self->source_compact = strdup(self->source); self->source_compact = deblank(self->source_compact); // here split because strlen in characters, // whereas each instruction code should 4 characters // we're converting char size num of instructions self->instructions_size = strlen(self->source_compact) / instruction_length; int i; (i = 0; < self->instructions_size; i++) { char *instr = substring(self->source_compact, i); if (strcmp(instr, error_code)) { // not equal nope if (self->instructions != null) { self->instructions = add_instructions(self->instructions, strtol(instr, null, 16)); } else { self->instructions = create_instructions(strtol(instr, null, 16)); } } } self->instructions = reverse(self->instructions); }

but i've added github link, since i'm not sure if it's function; or if it's due happening in entire source -- if c gurus can help me, brilliant :). i'm it's in either vm.c, or vm.h, , sorry terrible code; never looked much file io when learning (big mistake).

self->source = malloc(sizeof(char) * file_size); ... self->source[file_size] = '\0';

you need allocate 1 more byte terminating zero. index source[file_size] 1 byte beyond end of allocated memory. writing location may clobber other variable or internal structures used malloc(). create it:

self->source = malloc(sizeof(char) * (file_size + 1));

or just:

self->source = malloc(file_size + 1);

sizeof(char) 1, redundant.

you should check if allocation successful before trying access memory.

c file-io undefined-behavior

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -