Skip to content

Commit 1b1480f

Browse files
H. Peter AnvinJunio C Hamano
authored andcommitted
rename/copy score parsing updates.
Better variant, which handles stuff like "4.5%" and rejects "192.168.0.1". Additionally, make sure numbers are unsigned (I'm making them unsigned long just for the hell of it), to make sure that artificial wraparound scenarios don't cause harm. -hpa [jc: with this, -M100 changes its meaning back to 10%. People wanting to say "pure renames only" should now say -M100% or -M1.0; sounds a bit like an earthquake, but arguably things are more consistent this way ;-)] Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent f35230f commit 1b1480f

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

diff.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -838,29 +838,38 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
838838

839839
static int parse_num(const char **cp_p)
840840
{
841-
int num, scale, ch, cnt;
841+
unsigned long num, scale;
842+
int ch, dot;
842843
const char *cp = *cp_p;
843844

844-
cnt = num = 0;
845+
num = 0;
845846
scale = 1;
846-
while ('0' <= (ch = *cp) && ch <= '9') {
847-
if (cnt++ < 5) {
848-
/* We simply ignore more than 5 digits precision. */
849-
scale *= 10;
850-
num = num * 10 + ch - '0';
847+
dot = 0;
848+
for(;;) {
849+
ch = *cp;
850+
if ( !dot && ch == '.' ) {
851+
scale = 1;
852+
dot = 1;
853+
} else if ( ch == '%' ) {
854+
scale = dot ? scale*100 : 100;
855+
cp++; /* % is always at the end */
856+
break;
857+
} else if ( ch >= '0' && ch <= '9' ) {
858+
if ( scale < 100000 ) {
859+
scale *= 10;
860+
num = (num*10) + (ch-'0');
861+
}
862+
} else {
863+
break;
851864
}
852865
cp++;
853866
}
854867
*cp_p = cp;
855868

856-
/* special case: -M100 would mean 1.0 not 0.1 */
857-
if (num == 100 && scale == 1000)
858-
return MAX_SCORE;
859-
860869
/* user says num divided by scale and we say internally that
861870
* is MAX_SCORE * num / scale.
862871
*/
863-
return (MAX_SCORE * num / scale);
872+
return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
864873
}
865874

866875
int diff_scoreopt_parse(const char *opt)

0 commit comments

Comments
 (0)