From: j8takagi Date: Wed, 13 Feb 2013 13:41:33 +0000 (+0900) Subject: Valgrindで判明したメモリーに関する問題を修正 X-Git-Tag: v0.2p9~1 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=019b584feedf0402e473fba3e0e7752db78e616a;p=YACASL2.git Valgrindで判明したメモリーに関する問題を修正 --- diff --git a/src/assemble.c b/src/assemble.c index 8f09287..a69837f 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -184,9 +184,12 @@ WORD getliteral(const char *str, PASS pass) */ void writememory(WORD word, WORD adr, PASS pass) { + char *n; + /* メモリオーバーの場合、エラー発生 */ if(adr >= sys->memsize) { - setcerr(119, word2n(adr)); /* out of COMET II memory */ + setcerr(119, (n = word2n(adr))); /* out of COMET II memory */ + FREE(n) return; } (sys->memory)[adr] = word; diff --git a/src/dump.c b/src/dump.c index 769b175..ab418a8 100644 --- a/src/dump.c +++ b/src/dump.c @@ -1,5 +1,7 @@ #include + #include "exec.h" +#include "cmem.h" /** * COMET IIのメモリを表示する @@ -34,14 +36,19 @@ void dumpmemory() void dspregister() { int i; + char *sp, *pr, *fr; for(i = 0; i < GRSIZE; i++ ) { fprintf(stdout, "#%04X: GR%d: ", sys->cpu->pr, i); print_dumpword(sys->cpu->gr[i], (&execmode)->logical); } fprintf(stdout, "#%04X: SP: %6d = #%04X = %s\n", - sys->cpu->pr, sys->cpu->sp, sys->cpu->sp, word2bit(sys->cpu->sp)); + sys->cpu->pr, sys->cpu->sp, sys->cpu->sp, sp = word2bit(sys->cpu->sp)); fprintf(stdout, "#%04X: PR: %6d = #%04X = %s\n", - sys->cpu->pr, sys->cpu->pr, sys->cpu->pr, word2bit(sys->cpu->pr)); + sys->cpu->pr, sys->cpu->pr, sys->cpu->pr, pr = word2bit(sys->cpu->pr)); fprintf(stdout, "#%04X: FR (OF SF ZF): %s\n", - sys->cpu->pr, (word2bit(sys->cpu->fr)+13)); + sys->cpu->pr, ((fr = word2bit(sys->cpu->fr)) + 13)); + + FREE(sp); + FREE(pr); + FREE(fr); } diff --git a/src/exec.c b/src/exec.c index aa99076..c91fc86 100644 --- a/src/exec.c +++ b/src/exec.c @@ -158,8 +158,11 @@ void setfr(WORD adr) WORD get_r_r1(WORD oprx) { WORD r; + char *s; + if((r = ((oprx & 0x00F0) >>4)) > GRSIZE - 1) { - setcerr(205, pr2str(sys->cpu->pr)); /* r/r1 in word #1 - not GR */ + setcerr(205, s = pr2str(sys->cpu->pr)); /* r/r1 in word #1 - not GR */ + FREE(s); return 0x0; } return r; @@ -171,8 +174,11 @@ WORD get_r_r1(WORD oprx) WORD get_x_r2(WORD oprx) { WORD x; + char *s; + if((x = (oprx & 0x000F)) > GRSIZE - 1) { - setcerr(206, pr2str(sys->cpu->pr)); /* r/r1 in word #1 - not GR */ + setcerr(206, s = pr2str(sys->cpu->pr)); /* r/r1 in word #1 - not GR */ + FREE(s); return 0x0; } return x; @@ -197,8 +203,11 @@ WORD get_adr_x(WORD adr, WORD oprx) WORD get_val_adr_x(WORD adr, WORD oprx) { WORD a; + char *s; + if((a = get_adr_x(adr, oprx)) >= sys->memsize) { - setcerr(207, pr2str(sys->cpu->pr + 1)); /* address in word #2 - out of memory */ + setcerr(207, s = pr2str(sys->cpu->pr + 1)); /* address in word #2 - out of memory */ + FREE(s); return 0x0; } return sys->memory[a]; @@ -796,10 +805,11 @@ void jump() */ void push() { + assert(sys->cpu->sp > execptr->end && sys->cpu->sp <= sys->memsize); WORD w[2]; + w[0] = sys->memory[sys->cpu->pr]; w[1] = sys->memory[sys->cpu->pr + 1]; - assert(sys->cpu->sp > execptr->end && sys->cpu->sp <= sys->memsize); sys->memory[--(sys->cpu->sp)] = get_adr_x(w[1], w[0]); sys->cpu->pr += 2; } @@ -810,11 +820,18 @@ void push() */ void pop() { - assert(sys->cpu->sp > execptr->end && sys->cpu->sp <= sys->memsize); - WORD w[1]; - w[0] = sys->memory[sys->cpu->pr]; - sys->cpu->gr[get_r_r1(w[0])] = sys->memory[(sys->cpu->sp)++]; - sys->cpu->pr += 1; + assert(sys->cpu->sp > execptr->end); + WORD w; + char *s; + + if(sys->cpu->sp >= sys->memsize) { + setcerr(203, s = pr2str(sys->cpu->pr)); /* Stack Pointer (SP) - stack underflow */ + FREE(s); + } else { + w = sys->memory[sys->cpu->pr]; + sys->cpu->gr[get_r_r1(w)] = sys->memory[(sys->cpu->sp)++]; + sys->cpu->pr += 1; + } } /** @@ -877,6 +894,7 @@ void exec() { clock_t clock_begin, clock_end; void (*cmdptr)(); + char *s; create_code_type(); /* 命令のコードとタイプがキーのハッシュ表を作成 */ if(execmode.trace == true) { @@ -896,21 +914,23 @@ void exec() } fprintf(stdout, "\n"); } - /* プログラムレジスタとスタックポインタをチェック */ - if(sys->cpu->pr >= sys->memsize || sys->cpu->sp > sys->memsize || sys->cpu->sp <= execptr->end) { - if(sys->cpu->pr >= sys->memsize) { - setcerr(201, pr2str(sys->cpu->pr)); /* Program Register (PR) - memory overflow */ - } else if(sys->cpu->sp <= execptr->end) { - setcerr(202, pr2str(sys->cpu->pr)); /* Stack Pointer (SP) - stack overflow */ - } else if(sys->cpu->sp > sys->memsize) { - setcerr(203, pr2str(sys->cpu->pr)); /* Stack Pointer (SP) - stack underflow */ - } + /* プログラムレジスタをチェック */ + if(sys->cpu->pr >= sys->memsize) { + setcerr(201, s = pr2str(sys->cpu->pr)); /* Program Register (PR) - memory overflow */ + FREE(s); + goto execfin; + } + /* スタックポインタをチェック */ + if(sys->cpu->sp <= execptr->end) { + setcerr(202, s = pr2str(sys->cpu->pr)); /* Stack Pointer (SP) - stack overflow */ + FREE(s); goto execfin; } /* コードから命令を取得 */ /* 取得できない場合はエラー終了 */ if((cmdptr = getcmdptr(sys->memory[sys->cpu->pr] & 0xFF00)) == NULL) { - setcerr(204, pr2str(sys->cpu->pr)); /* OP in word #1 - not command code */ + setcerr(204, s = pr2str(sys->cpu->pr)); /* OP in word #1 - not command code */ + FREE(s); goto execfin; } /* 命令の実行 */ diff --git a/src/label.c b/src/label.c index efa52c3..c482cce 100644 --- a/src/label.c +++ b/src/label.c @@ -136,6 +136,9 @@ void printlabel() fprintf(stdout, "%s.", l[i]->prog); } fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr); + FREE(l[i]->prog); + FREE(l[i]->label); + FREE(l[i]); } FREE(l); } diff --git a/src/token.c b/src/token.c index 074b6c4..713a61f 100644 --- a/src/token.c +++ b/src/token.c @@ -131,8 +131,9 @@ CMDLINE *linetok(const char *line) /* 文字列が長すぎる場合はエラー */ if(strlen(p) > LABELSIZE) { setcerr(104, p); /* label length is too long */ + } else { + strcpy(cmdl->label, p); } - strcpy(cmdl->label, p); p = sepp + 1; } /* ラベルと命令の間の空白をスキップ */ diff --git a/src/word.c b/src/word.c index e355a67..34a3946 100644 --- a/src/word.c +++ b/src/word.c @@ -135,12 +135,14 @@ char *word2bit(const WORD word) */ void print_dumpword(WORD word, bool logicalmode) { + char *b; + if(logicalmode == true) { fprintf(stdout, "%6d", word); } else { fprintf(stdout, "%6d", (signed short)word); } - fprintf(stdout, " = #%04X = %s", word, word2bit(word)); + fprintf(stdout, " = #%04X = %s", word, (b = word2bit(word))); /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */ if(word >= 0x20 && word <= 0x7E) { fprintf(stdout, " = \'%c\'", word); @@ -150,4 +152,5 @@ void print_dumpword(WORD word, bool logicalmode) fprintf(stdout, " = \'\\t\'"); } fprintf(stdout, "\n"); + FREE(b); } diff --git a/test/system/casl2/err_203/0.txt b/test/system/casl2/err_203/0.txt index 5b6e1cf..403a8e7 100644 --- a/test/system/casl2/err_203/0.txt +++ b/test/system/casl2/err_203/0.txt @@ -3,4 +3,4 @@ MAIN START POP GR1 RET END -Execute error - 203: PR:#0001: Stack Pointer (SP) - stack underflow +Execute error - 203: PR:#0000: Stack Pointer (SP) - stack underflow diff --git a/test/system/comet2/err_203/0.txt b/test/system/comet2/err_203/0.txt index c903fa3..1ccd366 100644 --- a/test/system/comet2/err_203/0.txt +++ b/test/system/comet2/err_203/0.txt @@ -1 +1 @@ -Execute error - 203: PR:#0001: Stack Pointer (SP) - stack underflow +Execute error - 203: PR:#0000: Stack Pointer (SP) - stack underflow