From cddcd3839fa6cf69a46fe30a09a5b07fb66a70d4 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Thu, 8 Sep 2011 14:17:26 +0400 Subject: [PATCH] Resolve "comparison of signed and unsigned values" compiler warnings. (Apply commit 96f7891) * cord/cordprnt.c (CORD_vsprintf): Cast "prec" and "width" local variables to size_t. * dyn_load.c (GC_register_dynamic_libraries_dl_iterate_phdr): Change type of "i" local variable to int. * pthread_support.c (start_mark_threads): Likewise. * os_dep.c (GC_repeat_read): Change type of "num_read" to size_t. * os_dep.c (GC_get_maps): Change type of "result" local variable from int to ssize_t; cast "result" to size_t in comparison. * pthread_support.c (GC_remove_all_threads_but_me): Cast "result" to int. * pthread_support.c (GC_wait_for_gc_completion): Change type of "old_gc_no" local variable to word. * pthread_support.c (GC_lock): Change type of "i" local variable to unsigned. * tests/staticrootstest.c (main): Cast sizeof() value to int in comparisons. --- ChangeLog | 19 ++ cord/cordprnt.c | 499 ++++++++++++++++++++-------------------- dyn_load.c | 2 +- os_dep.c | 6 +- pthread_support.c | 8 +- tests/staticrootstest.c | 4 +- 6 files changed, 278 insertions(+), 260 deletions(-) diff --git a/ChangeLog b/ChangeLog index e03f8f60..e751f814 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2011-09-08 Ivan Maidanski + + * cord/cordprnt.c (CORD_vsprintf): Cast "prec" and "width" local + variables to size_t. + * dyn_load.c (GC_register_dynamic_libraries_dl_iterate_phdr): Change + type of "i" local variable to int. + * pthread_support.c (start_mark_threads): Likewise. + * os_dep.c (GC_repeat_read): Change type of "num_read" to size_t. + * os_dep.c (GC_get_maps): Change type of "result" local variable from + int to ssize_t; cast "result" to size_t in comparison. + * pthread_support.c (GC_remove_all_threads_but_me): Cast "result" to + int. + * pthread_support.c (GC_wait_for_gc_completion): Change type of + "old_gc_no" local variable to word. + * pthread_support.c (GC_lock): Change type of "i" local variable to + unsigned. + * tests/staticrootstest.c (main): Cast sizeof() value to int in + comparisons. + 2011-08-24 Ivan Maidanski * hpux_test_and_clear.s: Remove. diff --git a/cord/cordprnt.c b/cord/cordprnt.c index 6d278fed..f4c63868 100644 --- a/cord/cordprnt.c +++ b/cord/cordprnt.c @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved. * * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED @@ -10,17 +10,16 @@ * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. */ -/* An sprintf implementation that understands cords. This is probably */ -/* not terribly portable. It assumes an ANSI stdarg.h. It further */ -/* assumes that I can make copies of va_list variables, and read */ -/* arguments repeatedly by applyting va_arg to the copies. This */ -/* could be avoided at some performance cost. */ -/* We also assume that unsigned and signed integers of various kinds */ -/* have the same sizes, and can be cast back and forth. */ -/* We assume that void * and char * have the same size. */ -/* All this cruft is needed because we want to rely on the underlying */ -/* sprintf implementation whenever possible. */ -/* Boehm, September 21, 1995 6:00 pm PDT */ +/* An sprintf implementation that understands cords. This is probably */ +/* not terribly portable. It assumes an ANSI stdarg.h. It further */ +/* assumes that I can make copies of va_list variables, and read */ +/* arguments repeatedly by applying va_arg to the copies. This */ +/* could be avoided at some performance cost. */ +/* We also assume that unsigned and signed integers of various kinds */ +/* have the same sizes, and can be cast back and forth. */ +/* We assume that void * and char * have the same size. */ +/* All this cruft is needed because we want to rely on the underlying */ +/* sprintf implementation whenever possible. */ #include "cord.h" #include "ec.h" @@ -29,11 +28,11 @@ #include #include "gc.h" -#define CONV_SPEC_LEN 50 /* Maximum length of a single */ - /* conversion specification. */ -#define CONV_RESULT_LEN 50 /* Maximum length of any */ - /* conversion with default */ - /* width and prec. */ +#define CONV_SPEC_LEN 50 /* Maximum length of a single */ + /* conversion specification. */ +#define CONV_RESULT_LEN 50 /* Maximum length of any */ + /* conversion with default */ + /* width and prec. */ static int ec_len(CORD_ec x) @@ -41,20 +40,20 @@ static int ec_len(CORD_ec x) return(CORD_len(x[0].ec_cord) + (x[0].ec_bufptr - x[0].ec_buf)); } -/* Possible nonumeric precision values. */ +/* Possible nonumeric precision values. */ # define NONE -1 # define VARIABLE -2 -/* Copy the conversion specification from CORD_pos into the buffer buf */ -/* Return negative on error. */ -/* Source initially points one past the leading %. */ -/* It is left pointing at the conversion type. */ -/* Assign field width and precision to *width and *prec. */ -/* If width or prec is *, VARIABLE is assigned. */ -/* Set *left to 1 if left adjustment flag is present. */ -/* Set *long_arg to 1 if long flag ('l' or 'L') is present, or to */ -/* -1 if 'h' is present. */ +/* Copy the conversion specification from CORD_pos into the buffer buf */ +/* Return negative on error. */ +/* Source initially points one past the leading %. */ +/* It is left pointing at the conversion type. */ +/* Assign field width and precision to *width and *prec. */ +/* If width or prec is *, VARIABLE is assigned. */ +/* Set *left to 1 if left adjustment flag is present. */ +/* Set *long_arg to 1 if long flag ('l' or 'L') is present, or to */ +/* -1 if 'h' is present. */ static int extract_conv_spec(CORD_pos source, char *buf, - int * width, int *prec, int *left, int * long_arg) + int * width, int *prec, int *left, int * long_arg) { register int result = 0; register int current_number = 0; @@ -62,7 +61,7 @@ static int extract_conv_spec(CORD_pos source, char *buf, register int saw_number = 0; register int chars_so_far = 0; register char current; - + *width = NONE; buf[chars_so_far++] = '%'; while(CORD_pos_valid(source)) { @@ -70,73 +69,73 @@ static int extract_conv_spec(CORD_pos source, char *buf, current = CORD_pos_fetch(source); buf[chars_so_far++] = current; switch(current) { - case '*': - saw_number = 1; - current_number = VARIABLE; - break; + case '*': + saw_number = 1; + current_number = VARIABLE; + break; case '0': if (!saw_number) { /* Zero fill flag; ignore */ break; } /* otherwise fall through: */ case '1': - case '2': - case '3': - case '4': - case '5': + case '2': + case '3': + case '4': + case '5': case '6': - case '7': - case '8': - case '9': - saw_number = 1; - current_number *= 10; - current_number += current - '0'; - break; - case '.': - saw_period = 1; - if(saw_number) { - *width = current_number; - saw_number = 0; - } - current_number = 0; - break; - case 'l': - case 'L': - *long_arg = 1; - current_number = 0; - break; - case 'h': - *long_arg = -1; - current_number = 0; - break; - case ' ': - case '+': - case '#': - current_number = 0; - break; - case '-': - *left = 1; - current_number = 0; - break; - case 'd': - case 'i': - case 'o': - case 'u': - case 'x': - case 'X': - case 'f': - case 'e': - case 'E': - case 'g': - case 'G': - case 'c': - case 'C': - case 's': - case 'S': - case 'p': - case 'n': - case 'r': - goto done; + case '7': + case '8': + case '9': + saw_number = 1; + current_number *= 10; + current_number += current - '0'; + break; + case '.': + saw_period = 1; + if(saw_number) { + *width = current_number; + saw_number = 0; + } + current_number = 0; + break; + case 'l': + case 'L': + *long_arg = 1; + current_number = 0; + break; + case 'h': + *long_arg = -1; + current_number = 0; + break; + case ' ': + case '+': + case '#': + current_number = 0; + break; + case '-': + *left = 1; + current_number = 0; + break; + case 'd': + case 'i': + case 'o': + case 'u': + case 'x': + case 'X': + case 'f': + case 'e': + case 'E': + case 'g': + case 'G': + case 'c': + case 'C': + case 's': + case 'S': + case 'p': + case 'n': + case 'r': + goto done; default: return(-1); } @@ -145,14 +144,14 @@ static int extract_conv_spec(CORD_pos source, char *buf, return(-1); done: if (saw_number) { - if (saw_period) { - *prec = current_number; - } else { - *prec = NONE; - *width = current_number; - } + if (saw_period) { + *prec = current_number; + } else { + *prec = NONE; + *width = current_number; + } } else { - *prec = NONE; + *prec = NONE; } buf[chars_so_far] = '\0'; return(result); @@ -165,168 +164,168 @@ int CORD_vsprintf(CORD * out, CORD format, va_list args) register char current; CORD_pos pos; char conv_spec[CONV_SPEC_LEN + 1]; - + CORD_ec_init(result); for (CORD_set_pos(pos, format, 0); CORD_pos_valid(pos); CORD_next(pos)) { - current = CORD_pos_fetch(pos); - if (current == '%') { + current = CORD_pos_fetch(pos); + if (current == '%') { CORD_next(pos); if (!CORD_pos_valid(pos)) return(-1); current = CORD_pos_fetch(pos); if (current == '%') { - CORD_ec_append(result, current); + CORD_ec_append(result, current); } else { - int width, prec; - int left_adj = 0; - int long_arg = 0; - CORD arg; - size_t len; - - if (extract_conv_spec(pos, conv_spec, - &width, &prec, - &left_adj, &long_arg) < 0) { - return(-1); - } - current = CORD_pos_fetch(pos); - switch(current) { - case 'n': - /* Assign length to next arg */ - if (long_arg == 0) { - int * pos_ptr; - pos_ptr = va_arg(args, int *); - *pos_ptr = ec_len(result); - } else if (long_arg > 0) { - long * pos_ptr; - pos_ptr = va_arg(args, long *); - *pos_ptr = ec_len(result); - } else { - short * pos_ptr; - pos_ptr = va_arg(args, short *); - *pos_ptr = ec_len(result); - } - goto done; - case 'r': - /* Append cord and any padding */ - if (width == VARIABLE) width = va_arg(args, int); - if (prec == VARIABLE) prec = va_arg(args, int); - arg = va_arg(args, CORD); - len = CORD_len(arg); - if (prec != NONE && len > prec) { - if (prec < 0) return(-1); - arg = CORD_substr(arg, 0, prec); - len = prec; - } - if (width != NONE && len < width) { - char * blanks = GC_MALLOC_ATOMIC(width-len+1); + int width, prec; + int left_adj = 0; + int long_arg = 0; + CORD arg; + size_t len; + + if (extract_conv_spec(pos, conv_spec, + &width, &prec, + &left_adj, &long_arg) < 0) { + return(-1); + } + current = CORD_pos_fetch(pos); + switch(current) { + case 'n': + /* Assign length to next arg */ + if (long_arg == 0) { + int * pos_ptr; + pos_ptr = va_arg(args, int *); + *pos_ptr = ec_len(result); + } else if (long_arg > 0) { + long * pos_ptr; + pos_ptr = va_arg(args, long *); + *pos_ptr = ec_len(result); + } else { + short * pos_ptr; + pos_ptr = va_arg(args, short *); + *pos_ptr = ec_len(result); + } + goto done; + case 'r': + /* Append cord and any padding */ + if (width == VARIABLE) width = va_arg(args, int); + if (prec == VARIABLE) prec = va_arg(args, int); + arg = va_arg(args, CORD); + len = CORD_len(arg); + if (prec != NONE && len > (size_t)prec) { + if (prec < 0) return(-1); + arg = CORD_substr(arg, 0, prec); + len = prec; + } + if (width != NONE && len < (size_t)width) { + char * blanks = GC_MALLOC_ATOMIC(width-len+1); - memset(blanks, ' ', width-len); - blanks[width-len] = '\0'; - if (left_adj) { - arg = CORD_cat(arg, blanks); - } else { - arg = CORD_cat(blanks, arg); - } - } - CORD_ec_append_cord(result, arg); - goto done; - case 'c': - if (width == NONE && prec == NONE) { - register char c; + memset(blanks, ' ', width-len); + blanks[width-len] = '\0'; + if (left_adj) { + arg = CORD_cat(arg, blanks); + } else { + arg = CORD_cat(blanks, arg); + } + } + CORD_ec_append_cord(result, arg); + goto done; + case 'c': + if (width == NONE && prec == NONE) { + register char c; - c = (char)va_arg(args, int); - CORD_ec_append(result, c); - goto done; - } - break; - case 's': - if (width == NONE && prec == NONE) { - char * str = va_arg(args, char *); - register char c; + c = (char)va_arg(args, int); + CORD_ec_append(result, c); + goto done; + } + break; + case 's': + if (width == NONE && prec == NONE) { + char * str = va_arg(args, char *); + register char c; - while ((c = *str++)) { - CORD_ec_append(result, c); - } - goto done; - } - break; - default: - break; - } - /* Use standard sprintf to perform conversion */ - { - register char * buf; - va_list vsprintf_args; - int max_size = 0; - int res; -# ifdef __va_copy + while ((c = *str++)) { + CORD_ec_append(result, c); + } + goto done; + } + break; + default: + break; + } + /* Use standard sprintf to perform conversion */ + { + register char * buf; + va_list vsprintf_args; + int max_size = 0; + int res; +# ifdef __va_copy __va_copy(vsprintf_args, args); -# else -# if defined(__GNUC__) && !defined(__DJGPP__) /* and probably in other cases */ +# else +# if defined(__GNUC__) && !defined(__DJGPP__) /* and probably in other cases */ va_copy(vsprintf_args, args); -# else - vsprintf_args = args; -# endif -# endif - if (width == VARIABLE) width = va_arg(args, int); - if (prec == VARIABLE) prec = va_arg(args, int); - if (width != NONE) max_size = width; - if (prec != NONE && prec > max_size) max_size = prec; - max_size += CONV_RESULT_LEN; - if (max_size >= CORD_BUFSZ) { - buf = GC_MALLOC_ATOMIC(max_size + 1); - } else { - if (CORD_BUFSZ - (result[0].ec_bufptr-result[0].ec_buf) - < max_size) { - CORD_ec_flush_buf(result); - } - buf = result[0].ec_bufptr; - } - switch(current) { - case 'd': - case 'i': - case 'o': - case 'u': - case 'x': - case 'X': - case 'c': - if (long_arg <= 0) { - (void) va_arg(args, int); - } else if (long_arg > 0) { - (void) va_arg(args, long); - } - break; - case 's': - case 'p': - (void) va_arg(args, char *); - break; - case 'f': - case 'e': - case 'E': - case 'g': - case 'G': - (void) va_arg(args, double); - break; - default: - return(-1); - } - res = vsprintf(buf, conv_spec, vsprintf_args); - len = (size_t)res; - if ((char *)(GC_word)res == buf) { - /* old style vsprintf */ - len = strlen(buf); - } else if (res < 0) { - return(-1); - } - if (buf != result[0].ec_bufptr) { - register char c; +# else + vsprintf_args = args; +# endif +# endif + if (width == VARIABLE) width = va_arg(args, int); + if (prec == VARIABLE) prec = va_arg(args, int); + if (width != NONE) max_size = width; + if (prec != NONE && prec > max_size) max_size = prec; + max_size += CONV_RESULT_LEN; + if (max_size >= CORD_BUFSZ) { + buf = GC_MALLOC_ATOMIC(max_size + 1); + } else { + if (CORD_BUFSZ - (result[0].ec_bufptr-result[0].ec_buf) + < max_size) { + CORD_ec_flush_buf(result); + } + buf = result[0].ec_bufptr; + } + switch(current) { + case 'd': + case 'i': + case 'o': + case 'u': + case 'x': + case 'X': + case 'c': + if (long_arg <= 0) { + (void) va_arg(args, int); + } else if (long_arg > 0) { + (void) va_arg(args, long); + } + break; + case 's': + case 'p': + (void) va_arg(args, char *); + break; + case 'f': + case 'e': + case 'E': + case 'g': + case 'G': + (void) va_arg(args, double); + break; + default: + return(-1); + } + res = vsprintf(buf, conv_spec, vsprintf_args); + len = (size_t)res; + if ((char *)(GC_word)res == buf) { + /* old style vsprintf */ + len = strlen(buf); + } else if (res < 0) { + return(-1); + } + if (buf != result[0].ec_bufptr) { + register char c; - while ((c = *buf++)) { - CORD_ec_append(result, c); - } - } else { - result[0].ec_bufptr = buf + len; - } - } + while ((c = *buf++)) { + CORD_ec_append(result, c); + } + } else { + result[0].ec_bufptr = buf + len; + } + } done:; } } else { @@ -342,7 +341,7 @@ int CORD_sprintf(CORD * out, CORD format, ...) { va_list args; int result; - + va_start(args, format); result = CORD_vsprintf(out, format, args); va_end(args); @@ -354,7 +353,7 @@ int CORD_fprintf(FILE * f, CORD format, ...) va_list args; int result; CORD out; - + va_start(args, format); result = CORD_vsprintf(&out, format, args); va_end(args); @@ -366,7 +365,7 @@ int CORD_vfprintf(FILE * f, CORD format, va_list args) { int result; CORD out; - + result = CORD_vsprintf(&out, format, args); if (result > 0) CORD_put(out, f); return(result); @@ -377,7 +376,7 @@ int CORD_printf(CORD format, ...) va_list args; int result; CORD out; - + va_start(args, format); result = CORD_vsprintf(&out, format, args); va_end(args); @@ -389,7 +388,7 @@ int CORD_vprintf(CORD format, va_list args) { int result; CORD out; - + result = CORD_vsprintf(&out, format, args); if (result > 0) CORD_put(out, stdout); return(result); diff --git a/dyn_load.c b/dyn_load.c index 982ec379..3d5aad36 100644 --- a/dyn_load.c +++ b/dyn_load.c @@ -547,7 +547,7 @@ STATIC GC_bool GC_register_dynamic_libraries_dl_iterate_phdr(void) dl_iterate_phdr(GC_register_dynlib_callback, &did_something); if (did_something) { # ifdef PT_GNU_RELRO - size_t i; + int i; for (i = 0; i < n_load_segs; ++i) { if (load_segs[i].end > load_segs[i].start) { diff --git a/os_dep.c b/os_dep.c index 0290e2e0..1c0b73e1 100644 --- a/os_dep.c +++ b/os_dep.c @@ -149,7 +149,7 @@ /* we encounter EOF. */ STATIC ssize_t GC_repeat_read(int fd, char *buf, size_t count) { - ssize_t num_read = 0; + size_t num_read = 0; ssize_t result; ASSERT_CANCEL_DISABLED(); @@ -199,7 +199,7 @@ STATIC ssize_t GC_repeat_read(int fd, char *buf, size_t count) GC_INNER char * GC_get_maps(void) { int f; - int result; + ssize_t result; static char init_buf[1]; static char *maps_buf = init_buf; static size_t maps_buf_sz = 1; @@ -260,7 +260,7 @@ GC_INNER char * GC_get_maps(void) if (result <= 0) break; maps_size += result; - } while (result == maps_buf_sz-1); + } while ((size_t)result == maps_buf_sz-1); close(f); if (result <= 0) return 0; diff --git a/pthread_support.c b/pthread_support.c index 87317755..38f4476d 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -400,7 +400,7 @@ STATIC pthread_t GC_mark_threads[MAX_MARKERS]; static void start_mark_threads(void) { - unsigned i; + int i; pthread_attr_t attr; GC_ASSERT(I_DONT_HOLD_LOCK()); @@ -737,7 +737,7 @@ STATIC void GC_remove_all_threads_but_me(void) if (stat_buf[i] == '\n' && stat_buf[i+1] == 'c' && stat_buf[i+2] == 'p' && stat_buf[i+3] == 'u') { int cpu_no = atoi(&stat_buf[i + 4]); - if (cpu_no >= result) + if (cpu_no >= (int)result) result = cpu_no + 1; } } @@ -756,7 +756,7 @@ STATIC void GC_wait_for_gc_completion(GC_bool wait_for_all) GC_ASSERT(I_HOLD_LOCK()); ASSERT_CANCEL_DISABLED(); if (GC_incremental && GC_collection_in_progress()) { - int old_gc_no = GC_gc_no; + word old_gc_no = GC_gc_no; /* Make sure that no part of our stack is still on the mark stack, */ /* since it's about to be unmapped. */ @@ -1704,7 +1704,7 @@ GC_INNER void GC_lock(void) unsigned my_spin_max; static unsigned last_spins = 0; unsigned my_last_spins; - int i; + unsigned i; if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_CLEAR) { return; diff --git a/tests/staticrootstest.c b/tests/staticrootstest.c index 1f63fa24..222f2095 100644 --- a/tests/staticrootstest.c +++ b/tests/staticrootstest.c @@ -41,7 +41,7 @@ int main(void) root[i] = libsrl_mktree(12); GC_gcollect(); } - for (i = 0; i < sizeof(struct treenode); ++i) { + for (i = 0; i < (int)sizeof(struct treenode); ++i) { if (staticroot[i] != 0x42) return -1; } @@ -49,7 +49,7 @@ int main(void) root[i] = libsrl_mktree(12); GC_gcollect(); } - for (i = 0; i < sizeof(struct treenode); ++i) { + for (i = 0; i < (int)sizeof(struct treenode); ++i) { if (staticroot[i] != 0x42) return -1; } -- 2.40.0