나는 가상 머신을 작성해 왔으며 몇 년 전에이 함수를 작성 했음에도 이상한 일이 발생했다는 것을 알았다. 어쨌든, 내 가상 머신이 같은 파일을 읽 나는 0000 또는 새 줄 뒤에 공백이없는 할 때, 그러나C 파일의 이상한 동작 IO
0002 000A 0001 0004 0000
를 ... 그것은 충돌합니다. 또 다른 정말 이상한 일은, 그것이 이 아니고, 파일 로딩이이고 파일에서 공백을 제거하면 ... 0000
입니다. 나는 GDB를 통해 실행 해 보았지만, 실제로 작동한다 - 이것은 분명히 heisenbug 또는 무엇이라고 불린다. 제 생각에는 파일이로드되는 방식인데, 이는 this function here on github에서 볼 수 있습니다. 아니면 아래의 스 니펫을 읽으십시오.
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 is 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 we divide because strlen is in characters,
// whereas each instruction code should be 4 characters
// so we're converting char size to num of instructions
self->instructions_size = strlen(self->source_compact)/INSTRUCTION_LENGTH;
int i;
for (i = 0; i < self->instructions_size; i++) {
char *instr = substring(self->source_compact, i);
if (strcmp(instr, ERROR_CODE)) { // not equal to 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);
}
하지만 그것은 그 함수인지 확실하지 않다 때문에 내가하는 GitHub의 링크를 추가했습니다; 또는 그것이 전체 출처에서 일어나는 일 때문에 - C 전문가가 저를 도울 수 있다면, 그것은 훌륭할 것입니다 :). 나는 그것이 vm.c
, 또는 vm.h
에있는 것으로 확신하며, 끔찍한 코드를 유감스럽게 생각합니다. 필자가 (큰 실수)를 배웠을 때 나는 File IO에 대해 많이 보지 못했습니다.
'self-> source [file_size] = '\ 0'; '범위를 벗어난 액세스. – BLUEPIXY
'file_size - 1'이 될 것인가, 아니면 malloc이해야합니까 (file_size + 1)? – dean
'malloc (sizeof (char) * (file_size + 1)); ' – BLUEPIXY