ruby-changes:30841
From: nobu <ko1@a...>
Date: Thu, 12 Sep 2013 22:37:18 +0900 (JST)
Subject: [ruby-changes:30841] nobu:r42920 (trunk): Eliminate less-than-zero checks for unsigned variables
nobu 2013-09-12 22:37:11 +0900 (Thu, 12 Sep 2013) New Revision: 42920 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42920 Log: Eliminate less-than-zero checks for unsigned variables * ext/bigdecimal/bigdecimal.c, ext/digest/md5/md5.c, ext/json/fbuffer/fbuffer.h, ext/json/generator/generator.c: Eliminate less-than-zero checks for unsigned variables. According to section 4.1.5 of C89 standard, size_t is an unsigned type. These checks were found with 'cppcheck' static analysis tool. [ruby-core:57117] [Feature #8890] Modified files: trunk/ChangeLog trunk/ext/bigdecimal/bigdecimal.c trunk/ext/digest/md5/md5.c trunk/ext/json/fbuffer/fbuffer.h trunk/ext/json/generator/generator.c Index: ChangeLog =================================================================== --- ChangeLog (revision 42919) +++ ChangeLog (revision 42920) @@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Sep 12 22:37:08 2013 Anton Ovchinnikov <revolver112@g...> + + * ext/bigdecimal/bigdecimal.c, ext/digest/md5/md5.c, + ext/json/fbuffer/fbuffer.h, ext/json/generator/generator.c: + Eliminate less-than-zero checks for unsigned variables. + According to section 4.1.5 of C89 standard, size_t is an unsigned + type. These checks were found with 'cppcheck' static analysis tool. + [ruby-core:57117] [Feature #8890] + Thu Sep 12 21:35:46 2013 Naohisa Goto <ngotogenome@g...> * Makefile.in (libruby-static.a): change LDFLAGS order. LDFLAGS may Index: ext/digest/md5/md5.c =================================================================== --- ext/digest/md5/md5.c (revision 42919) +++ ext/digest/md5/md5.c (revision 42920) @@ -368,7 +368,7 @@ MD5_Update(MD5_CTX *pms, const uint8_t * https://github.com/ruby/ruby/blob/trunk/ext/digest/md5/md5.c#L368 size_t offset = (pms->count[0] >> 3) & 63; uint32_t nbits = (uint32_t)(nbytes << 3); - if (nbytes <= 0) + if (nbytes == 0) return; /* Update the message length. */ Index: ext/bigdecimal/bigdecimal.c =================================================================== --- ext/bigdecimal/bigdecimal.c (revision 42919) +++ ext/bigdecimal/bigdecimal.c (revision 42920) @@ -3936,7 +3936,7 @@ VpAlloc(size_t mx, const char *szVal) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L3936 } nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */ /* units for szVal[] */ - if (mx <= 0) mx = 1; + if (mx == 0) mx = 1; nalloc = Max(nalloc, mx); mx = nalloc; vp = VpAllocReal(mx); @@ -5029,7 +5029,7 @@ VpFormatSt(char *psz, size_t fFmt) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L5029 size_t ie, i, nf = 0; char ch; - if (fFmt <= 0) return; + if (fFmt == 0) return; ie = strlen(psz); for (i = 0; i < ie; ++i) { @@ -6162,12 +6162,12 @@ VpVarCheck(Real * v) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L6162 { size_t i; - if (v->MaxPrec <= 0) { + if (v->MaxPrec == 0) { printf("ERROR(VpVarCheck): Illegal Max. Precision(=%"PRIuSIZE")\n", v->MaxPrec); return 1; } - if (v->Prec <= 0 || v->Prec > v->MaxPrec) { + if (v->Prec == 0 || v->Prec > v->MaxPrec) { printf("ERROR(VpVarCheck): Illegal Precision(=%"PRIuSIZE")\n", v->Prec); printf(" Max. Prec.=%"PRIuSIZE"\n", v->MaxPrec); return 2; Index: ext/json/fbuffer/fbuffer.h =================================================================== --- ext/json/fbuffer/fbuffer.h (revision 42919) +++ ext/json/fbuffer/fbuffer.h (revision 42920) @@ -67,7 +67,7 @@ static VALUE fbuffer_to_s(FBuffer *fb); https://github.com/ruby/ruby/blob/trunk/ext/json/fbuffer/fbuffer.h#L67 static FBuffer *fbuffer_alloc(unsigned long initial_length) { FBuffer *fb; - if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT; + if (initial_length == 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT; fb = ALLOC(FBuffer); memset((void *) fb, 0, sizeof(FBuffer)); fb->initial_length = initial_length; Index: ext/json/generator/generator.c =================================================================== --- ext/json/generator/generator.c (revision 42919) +++ ext/json/generator/generator.c (revision 42920) @@ -288,7 +288,7 @@ static void convert_UTF8_to_JSON(FBuffer https://github.com/ruby/ruby/blob/trunk/ext/json/generator/generator.c#L288 static char *fstrndup(const char *ptr, unsigned long len) { char *result; - if (len <= 0) return NULL; + if (len == 0) return NULL; result = ALLOC_N(char, len); memccpy(result, ptr, 0, len); return result; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/