From: Ivan Maidanski Date: Sat, 27 Aug 2016 05:48:02 +0000 (+0300) Subject: Eliminate 'scope of variable can be reduced' cppcheck warnings X-Git-Tag: v8.0.0~1161 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=224ecfa5ed62b575376267c4168c3f5e6daf69cf;p=platform%2Fupstream%2Flibgc.git Eliminate 'scope of variable can be reduced' cppcheck warnings * cord/cordbscs.c (CORD_cat_char_star, CORD_from_fn, CORD_substr_checked, CORD_riter4, CORD_init_min_len): Move local variable declaration to the inner scope where the variable is used. * cord/cordxtra.c (CORD_cmp, CORD_ncmp, CORD_from_file_eager): Likewise. * cord/tests/cordtest.c (test_basics): Likewise. * cord/tests/de.c (line_pos, replace_line): Likewise. * dbg_mlc.c (GC_generate_random_heap_address): Likewise. * mark.c (GC_print_trace_inner): Likewise. * misc.c (GC_printf): Likewise. * cord/cordbscs.c (CORD_cat_char_star, CORD_from_fn, CORD_substr_checked, CORD_riter4, CORD_init_min_len): Remove "register" keyword for the moved variable. * cord/cordxtra.c (CORD_cmp, CORD_ncmp, CORD_from_file_eager): Likewise. * cord/tests/de.c (replace_line): Likewise. --- diff --git a/cord/cordbscs.c b/cord/cordbscs.c index 5aeb3f8..2c54d06 100644 --- a/cord/cordbscs.c +++ b/cord/cordbscs.c @@ -175,13 +175,14 @@ CORD CORD_cat_char_star(CORD x, const char * y, size_t leny) register CORD right; register CORD left; register char * new_right; - register size_t right_len; lenx = LEN(x); if (leny <= SHORT_LIMIT/2 && IS_CONCATENATION(x) && CORD_IS_STRING(right = ((CordRep *)x) -> concatenation.right)) { + size_t right_len; + /* Merge y into right part of x. */ if (!CORD_IS_STRING(left = ((CordRep *)x) -> concatenation.left)) { right_len = lenx - LEN(left); @@ -285,10 +286,10 @@ CORD CORD_from_fn(CORD_fn fn, void * client_data, size_t len) register char * result; register size_t i; char buf[SHORT_LIMIT+1]; - register char c; for (i = 0; i < len; i++) { - c = (*fn)(i, client_data); + char c = (*fn)(i, client_data); + if (c == '\0') goto gen_case; buf[i] = c; } @@ -432,12 +433,12 @@ CORD CORD_substr_checked(CORD x, size_t i, size_t n) register struct Function * f = &(((CordRep *)x) -> function); char buf[SUBSTR_LIMIT+1]; register char * p = buf; - register char c; register int j; register int lim = i + n; for (j = i; j < lim; j++) { - c = (*(f -> fn))(j, f -> client_data); + char c = (*(f -> fn))(j, f -> client_data); + if (c == '\0') { return(CORD_substr_closure(x, i, n, CORD_apply_access_fn)); } @@ -523,10 +524,10 @@ int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data) if (x == 0) return(0); if (CORD_IS_STRING(x)) { register const char *p = x + i; - register char c; for(;;) { - c = *p; + char c = *p; + if (c == '\0') ABORT("2nd arg to CORD_riter4 too big"); if ((*f1)(c, client_data)) return(1); if (p == x) break; @@ -603,12 +604,13 @@ typedef ForestElement Forest [ MAX_DEPTH ]; void CORD_init_min_len(void) { register int i; - register size_t last, previous, current; + size_t last, previous; min_len[0] = previous = 1; min_len[1] = last = 2; for (i = 2; i < MAX_DEPTH; i++) { - current = last + previous; + size_t current = last + previous; + if (current < last) /* overflow */ current = last; min_len[i] = current; previous = last; diff --git a/cord/cordxtra.c b/cord/cordxtra.c index a185185..79abca0 100644 --- a/cord/cordxtra.c +++ b/cord/cordxtra.c @@ -153,7 +153,6 @@ int CORD_cmp(CORD x, CORD y) { CORD_pos xpos; CORD_pos ypos; - register size_t avail, yavail; if (y == CORD_EMPTY) return(x != CORD_EMPTY); if (x == CORD_EMPTY) return(-1); @@ -161,6 +160,8 @@ int CORD_cmp(CORD x, CORD y) CORD_set_pos(xpos, x, 0); CORD_set_pos(ypos, y, 0); for(;;) { + size_t avail, yavail; + if (!CORD_pos_valid(xpos)) { if (CORD_pos_valid(ypos)) { return(-1); @@ -197,11 +198,12 @@ int CORD_ncmp(CORD x, size_t x_start, CORD y, size_t y_start, size_t len) CORD_pos xpos; CORD_pos ypos; register size_t count; - register long avail, yavail; CORD_set_pos(xpos, x, x_start); CORD_set_pos(ypos, y, y_start); for(count = 0; count < len;) { + long avail, yavail; + if (!CORD_pos_valid(xpos)) { if (CORD_pos_valid(ypos)) { return(-1); @@ -456,12 +458,12 @@ CORD CORD_chars(char c, size_t i) CORD CORD_from_file_eager(FILE * f) { - register int c; CORD_ec ecord; CORD_ec_init(ecord); for(;;) { - c = getc(f); + int c = getc(f); + if (c == 0) { /* Append the right number of NULs */ /* Note that any string of NULs is represented in 4 words, */ diff --git a/cord/tests/cordtest.c b/cord/tests/cordtest.c index 061430d..3e2092a 100644 --- a/cord/tests/cordtest.c +++ b/cord/tests/cordtest.c @@ -53,7 +53,6 @@ void test_basics(void) { CORD x = CORD_from_char_star("ab"); register int i; - char c; CORD y; CORD_pos p; @@ -114,9 +113,11 @@ void test_basics(void) i = 0; CORD_set_pos(p, y, i); while(CORD_pos_valid(p)) { - c = CORD_pos_fetch(p); + char c = CORD_pos_fetch(p); + if(c != i) ABORT("Traversal of function node failed"); - CORD_next(p); i++; + CORD_next(p); + i++; } if (i != 13) ABORT("Bad apparent length for function node"); } diff --git a/cord/tests/de.c b/cord/tests/de.c index 84aa4de..096e766 100644 --- a/cord/tests/de.c +++ b/cord/tests/de.c @@ -164,7 +164,6 @@ size_t line_pos(int i, int *c) { int j; size_t cur; - size_t next; line_map map = current_map; while (map -> line > i) map = map -> previous; @@ -176,7 +175,8 @@ size_t line_pos(int i, int *c) if (++j > current_map -> line) add_map(j, cur); } if (c != 0) { - next = CORD_chr(current, cur, '\n'); + size_t next = CORD_chr(current, cur, '\n'); + if (next == CORD_NOT_FOUND) next = current_len - 1; if (next < cur + *c) { *c = next - cur; @@ -216,7 +216,6 @@ int screen_size = 0; /* terribly appropriate for tabs. */ void replace_line(int i, CORD s) { - register int c; CORD_pos p; # if !defined(MACINTOSH) size_t len = CORD_len(s); @@ -237,7 +236,8 @@ void replace_line(int i, CORD s) move(i, 0); clrtoeol(); move(i,0); CORD_FOR (p, s) { - c = CORD_pos_fetch(p) & 0x7f; + int c = CORD_pos_fetch(p) & 0x7f; + if (iscntrl(c)) { standout(); addch(c + 0x40); standend(); } else { diff --git a/dbg_mlc.c b/dbg_mlc.c index 37c8f42..02e36de 100644 --- a/dbg_mlc.c +++ b/dbg_mlc.c @@ -142,7 +142,6 @@ GC_API void * GC_CALL GC_generate_random_heap_address(void) { size_t i; - size_t size; word heap_offset = RANDOM(); if (GC_heapsize > RAND_MAX) { @@ -154,6 +153,8 @@ /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */ /* it's not too bad. */ for (i = 0;; ++i) { + size_t size; + if (i >= GC_n_heap_sects) ABORT("GC_generate_random_heap_address: size inconsistency"); diff --git a/mark.c b/mark.c index 0c61add..e8d46ba 100644 --- a/mark.c +++ b/mark.c @@ -1486,9 +1486,10 @@ void GC_add_trace_entry(char *kind, word arg1, word arg2) void GC_print_trace_inner(word gc_no) { int i; - struct trace_entry *p; for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) { + struct trace_entry *p; + if (i < 0) i = TRACE_ENTRIES-1; p = GC_trace_buf + i; if (p -> gc_no < gc_no || p -> kind == 0) { diff --git a/misc.c b/misc.c index 57bb663..baefd03 100644 --- a/misc.c +++ b/misc.c @@ -1621,9 +1621,9 @@ GC_API void GC_CALL GC_enable_incremental(void) void GC_printf(const char *format, ...) { - char buf[BUFSZ + 1]; - if (!GC_quiet) { + char buf[BUFSZ + 1]; + GC_PRINTF_FILLBUF(buf, format); # ifdef NACL (void)WRITE(GC_stdout, buf, strlen(buf));