Skip to content

Commit 26affdf

Browse files
committed
---
yaml --- r: 41713 b: refs/heads/master c: a157f1d h: refs/heads/master i: 41711: c8e6f47 v: v3
1 parent ca31100 commit 26affdf

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 406d2b3bfe6336c71b6aedd202bb896fc47b2587
2+
refs/heads/master: a157f1d5bf86c1d929cccb5d1b5b197d3147b760
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libstd/time.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
320320
Some((value, pos))
321321
}
322322

323+
fn match_digits_in_range(ss: &str, pos: uint, digits: uint, ws: bool, min: i32, max: i32)
324+
-> Option<(i32, uint)> {
325+
match match_digits(ss, pos, digits, ws) {
326+
Some((val, pos)) if val >= min && val <= max => {
327+
Some((val, pos))
328+
}
329+
_ => None
330+
}
331+
}
332+
323333
fn parse_char(s: &str, pos: uint, c: char) -> Result<uint, ~str> {
324334
let range = str::char_range_at(s, pos);
325335

@@ -393,7 +403,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
393403
Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
394404
None => Err(~"Invalid month")
395405
},
396-
'C' => match match_digits(s, pos, 2u, false) {
406+
'C' => match match_digits_in_range(s, pos, 2u, false, 0_i32, 99_i32) {
397407
Some(item) => {
398408
let (v, pos) = item;
399409
tm.tm_year += (v * 100_i32) - 1900_i32;
@@ -419,11 +429,11 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
419429
.chain(|pos| parse_char(s, pos, '/'))
420430
.chain(|pos| parse_type(s, pos, 'y', tm))
421431
}
422-
'd' => match match_digits(s, pos, 2u, false) {
432+
'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32, 31_i32) {
423433
Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
424434
None => Err(~"Invalid day of the month")
425435
},
426-
'e' => match match_digits(s, pos, 2u, true) {
436+
'e' => match match_digits_in_range(s, pos, 2u, true, 1_i32, 31_i32) {
427437
Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
428438
None => Err(~"Invalid day of the month")
429439
},
@@ -435,15 +445,13 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
435445
.chain(|pos| parse_type(s, pos, 'd', tm))
436446
}
437447
'H' => {
438-
// FIXME (#2350): range check.
439-
match match_digits(s, pos, 2u, false) {
448+
match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) {
440449
Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
441450
None => Err(~"Invalid hour")
442451
}
443452
}
444453
'I' => {
445-
// FIXME (#2350): range check.
446-
match match_digits(s, pos, 2u, false) {
454+
match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
447455
Some(item) => {
448456
let (v, pos) = item;
449457
tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
@@ -453,8 +461,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
453461
}
454462
}
455463
'j' => {
456-
// FIXME (#2350): range check.
457-
match match_digits(s, pos, 3u, false) {
464+
match match_digits_in_range(s, pos, 3u, false, 1_i32, 366_i32) {
458465
Some(item) => {
459466
let (v, pos) = item;
460467
tm.tm_yday = v - 1_i32;
@@ -464,15 +471,13 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
464471
}
465472
}
466473
'k' => {
467-
// FIXME (#2350): range check.
468-
match match_digits(s, pos, 2u, true) {
474+
match match_digits_in_range(s, pos, 2u, true, 0_i32, 23_i32) {
469475
Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
470476
None => Err(~"Invalid hour")
471477
}
472478
}
473479
'l' => {
474-
// FIXME (#2350): range check.
475-
match match_digits(s, pos, 2u, true) {
480+
match match_digits_in_range(s, pos, 2u, true, 1_i32, 12_i32) {
476481
Some(item) => {
477482
let (v, pos) = item;
478483
tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
@@ -482,15 +487,13 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
482487
}
483488
}
484489
'M' => {
485-
// FIXME (#2350): range check.
486-
match match_digits(s, pos, 2u, false) {
490+
match match_digits_in_range(s, pos, 2u, false, 0_i32, 59_i32) {
487491
Some(item) => { let (v, pos) = item; tm.tm_min = v; Ok(pos) }
488492
None => Err(~"Invalid minute")
489493
}
490494
}
491495
'm' => {
492-
// FIXME (#2350): range check.
493-
match match_digits(s, pos, 2u, false) {
496+
match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
494497
Some(item) => {
495498
let (v, pos) = item;
496499
tm.tm_mon = v - 1_i32;
@@ -527,8 +530,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
527530
.chain(|pos| parse_type(s, pos, 'p', tm))
528531
}
529532
'S' => {
530-
// FIXME (#2350): range check.
531-
match match_digits(s, pos, 2u, false) {
533+
match match_digits_in_range(s, pos, 2u, false, 0_i32, 60_i32) {
532534
Some(item) => {
533535
let (v, pos) = item;
534536
tm.tm_sec = v;
@@ -547,8 +549,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
547549
}
548550
't' => parse_char(s, pos, '\t'),
549551
'u' => {
550-
// FIXME (#2350): range check.
551-
match match_digits(s, pos, 1u, false) {
552+
match match_digits_in_range(s, pos, 1u, false, 1_i32, 7_i32) {
552553
Some(item) => {
553554
let (v, pos) = item;
554555
tm.tm_wday = v-1_i32;
@@ -566,16 +567,14 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
566567
}
567568
//'W' {}
568569
'w' => {
569-
// FIXME (#2350): range check.
570-
match match_digits(s, pos, 1u, false) {
570+
match match_digits_in_range(s, pos, 1u, false, 0_i32, 6_i32) {
571571
Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
572572
None => Err(~"Invalid day of week")
573573
}
574574
}
575575
//'X' {}
576576
//'x' {}
577577
'Y' => {
578-
// FIXME (#2350): range check.
579578
match match_digits(s, pos, 4u, false) {
580579
Some(item) => {
581580
let (v, pos) = item;
@@ -586,8 +585,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
586585
}
587586
}
588587
'y' => {
589-
// FIXME (#2350): range check.
590-
match match_digits(s, pos, 2u, false) {
588+
match match_digits_in_range(s, pos, 2u, false, 0_i32, 99_i32) {
591589
Some(item) => {
592590
let (v, pos) = item;
593591
tm.tm_year = v;

0 commit comments

Comments
 (0)