Skip to content

Commit 0e3994f

Browse files
Junio C HamanoLinus Torvalds
authored andcommitted
[PATCH] diff: Clean up diff_scoreopt_parse().
This cleans up diff_scoreopt_parse() function that is used to parse the fractional notation -B, -C and -M option takes. The callers are modified to check for errors and complain. Earlier they silently ignored malformed input and falled back on the default. Signed-off-by: Junio C Hamano <junkio@cox.net> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent ce24067 commit 0e3994f

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
lines changed

diff-cache.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,20 @@ int main(int argc, const char **argv)
191191
continue;
192192
}
193193
if (!strncmp(arg, "-B", 2)) {
194-
diff_break_opt = diff_scoreopt_parse(arg);
194+
if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
195+
usage(diff_cache_usage);
195196
continue;
196197
}
197198
if (!strncmp(arg, "-M", 2)) {
198199
detect_rename = DIFF_DETECT_RENAME;
199-
diff_score_opt = diff_scoreopt_parse(arg);
200+
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
201+
usage(diff_cache_usage);
200202
continue;
201203
}
202204
if (!strncmp(arg, "-C", 2)) {
203205
detect_rename = DIFF_DETECT_COPY;
204-
diff_score_opt = diff_scoreopt_parse(arg);
206+
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
207+
usage(diff_cache_usage);
205208
continue;
206209
}
207210
if (!strcmp(arg, "-z")) {

diff-files.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,21 @@ int main(int argc, const char **argv)
6161
orderfile = argv[1] + 2;
6262
else if (!strcmp(argv[1], "--pickaxe-all"))
6363
pickaxe_opts = DIFF_PICKAXE_ALL;
64-
else if (!strncmp(argv[1], "-B", 2))
65-
diff_break_opt = diff_scoreopt_parse(argv[1]);
64+
else if (!strncmp(argv[1], "-B", 2)) {
65+
if ((diff_break_opt =
66+
diff_scoreopt_parse(argv[1])) == -1)
67+
usage(diff_files_usage);
68+
}
6669
else if (!strncmp(argv[1], "-M", 2)) {
67-
diff_score_opt = diff_scoreopt_parse(argv[1]);
70+
if ((diff_score_opt =
71+
diff_scoreopt_parse(argv[1])) == -1)
72+
usage(diff_files_usage);
6873
detect_rename = DIFF_DETECT_RENAME;
6974
}
7075
else if (!strncmp(argv[1], "-C", 2)) {
71-
diff_score_opt = diff_scoreopt_parse(argv[1]);
76+
if ((diff_score_opt =
77+
diff_scoreopt_parse(argv[1])) == -1)
78+
usage(diff_files_usage);
7279
detect_rename = DIFF_DETECT_COPY;
7380
}
7481
else

diff-tree.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,19 @@ int main(int argc, const char **argv)
459459
}
460460
if (!strncmp(arg, "-M", 2)) {
461461
detect_rename = DIFF_DETECT_RENAME;
462-
diff_score_opt = diff_scoreopt_parse(arg);
462+
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
463+
usage(diff_tree_usage);
463464
continue;
464465
}
465466
if (!strncmp(arg, "-C", 2)) {
466467
detect_rename = DIFF_DETECT_COPY;
467-
diff_score_opt = diff_scoreopt_parse(arg);
468+
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
469+
usage(diff_tree_usage);
468470
continue;
469471
}
470472
if (!strncmp(arg, "-B", 2)) {
471-
diff_break_opt = diff_scoreopt_parse(arg);
473+
if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
474+
usage(diff_tree_usage);
472475
continue;
473476
}
474477
if (!strcmp(arg, "-z")) {

diff.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,45 @@ void diff_setup(int flags)
589589

590590
}
591591

592+
static int parse_num(const char **cp_p)
593+
{
594+
int num, scale, ch, cnt;
595+
const char *cp = *cp_p;
596+
597+
cnt = num = 0;
598+
scale = 1;
599+
while ('0' <= (ch = *cp) && ch <= '9') {
600+
if (cnt++ < 5) {
601+
/* We simply ignore more than 5 digits precision. */
602+
scale *= 10;
603+
num = num * 10 + ch - '0';
604+
}
605+
*cp++;
606+
}
607+
*cp_p = cp;
608+
609+
/* user says num divided by scale and we say internally that
610+
* is MAX_SCORE * num / scale.
611+
*/
612+
return (MAX_SCORE * num / scale);
613+
}
614+
615+
int diff_scoreopt_parse(const char *opt)
616+
{
617+
int opt1, cmd;
618+
619+
if (*opt++ != '-')
620+
return -1;
621+
cmd = *opt++;
622+
if (cmd != 'M' && cmd != 'C' && cmd != 'B')
623+
return -1; /* that is not a -M, -C nor -B option */
624+
625+
opt1 = parse_num(&opt);
626+
if (*opt != 0)
627+
return -1;
628+
return opt1;
629+
}
630+
592631
struct diff_queue_struct diff_queued_diff;
593632

594633
void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)

diffcore-rename.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,24 +229,6 @@ static int score_compare(const void *a_, const void *b_)
229229
return b->score - a->score;
230230
}
231231

232-
int diff_scoreopt_parse(const char *opt)
233-
{
234-
int diglen, num, scale, i;
235-
if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C' && opt[1] != 'B'))
236-
return -1; /* that is not a -M, -C nor -B option */
237-
diglen = strspn(opt+2, "0123456789");
238-
if (diglen == 0 || strlen(opt+2) != diglen)
239-
return 0; /* use default */
240-
sscanf(opt+2, "%d", &num);
241-
for (i = 0, scale = 1; i < diglen; i++)
242-
scale *= 10;
243-
244-
/* user says num divided by scale and we say internally that
245-
* is MAX_SCORE * num / scale.
246-
*/
247-
return MAX_SCORE * num / scale;
248-
}
249-
250232
void diffcore_rename(int detect_rename, int minimum_score)
251233
{
252234
struct diff_queue_struct *q = &diff_queued_diff;

0 commit comments

Comments
 (0)