|
| 1 | +// This file is part of ICU4X. For terms of use, please see the file |
| 2 | +// called LICENSE at the top level of the ICU4X source tree |
| 3 | +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 | + |
| 5 | +#![no_main] |
| 6 | + |
| 7 | +use arbitrary::Arbitrary; |
| 8 | +use icu_calendar::options::*; |
| 9 | +use icu_calendar::types::{DateFields, MonthCode}; |
| 10 | +use icu_calendar::{AnyCalendar, Date}; |
| 11 | +use libfuzzer_sys::fuzz_target; |
| 12 | +use std::num::NonZeroU8; |
| 13 | + |
| 14 | +#[derive(Arbitrary, Debug)] |
| 15 | +struct FuzzInput { |
| 16 | + year: i32, |
| 17 | + month: u8, |
| 18 | + day: u8, |
| 19 | + month_interpretation: MonthInterpretation, |
| 20 | + overflow_constrain: bool, |
| 21 | + cal: AnyCalendarKind, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Arbitrary, Debug)] |
| 25 | +enum MonthInterpretation { |
| 26 | + Ordinal, |
| 27 | + CodeNormal, |
| 28 | + CodeLeap, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Arbitrary, Debug)] |
| 32 | +pub enum AnyCalendarKind { |
| 33 | + Buddhist, |
| 34 | + Chinese, |
| 35 | + Coptic, |
| 36 | + Dangi, |
| 37 | + Ethiopian, |
| 38 | + EthiopianAmeteAlem, |
| 39 | + Gregorian, |
| 40 | + Hebrew, |
| 41 | + Indian, |
| 42 | + HijriTabularTypeIIFriday, |
| 43 | + // Not needed by Temporal and has some bugs |
| 44 | + // https://github.com/unicode-org/icu4x/issues/7049#issuecomment-3384358307 |
| 45 | + // HijriSimulatedMecca, |
| 46 | + HijriTabularTypeIIThursday, |
| 47 | + HijriUmmAlQura, |
| 48 | + Iso, |
| 49 | + Japanese, |
| 50 | + JapaneseExtended, |
| 51 | + Persian, |
| 52 | + Roc, |
| 53 | + // Note: This doesn't cover Julian, since it's not in AnyCalendar |
| 54 | +} |
| 55 | + |
| 56 | +impl From<AnyCalendarKind> for icu_calendar::AnyCalendarKind { |
| 57 | + fn from(other: AnyCalendarKind) -> Self { |
| 58 | + match other { |
| 59 | + AnyCalendarKind::Buddhist => Self::Buddhist, |
| 60 | + AnyCalendarKind::Chinese => Self::Chinese, |
| 61 | + AnyCalendarKind::Coptic => Self::Coptic, |
| 62 | + AnyCalendarKind::Dangi => Self::Dangi, |
| 63 | + AnyCalendarKind::Ethiopian => Self::Ethiopian, |
| 64 | + AnyCalendarKind::EthiopianAmeteAlem => Self::EthiopianAmeteAlem, |
| 65 | + AnyCalendarKind::Gregorian => Self::Gregorian, |
| 66 | + AnyCalendarKind::Hebrew => Self::Hebrew, |
| 67 | + AnyCalendarKind::Indian => Self::Indian, |
| 68 | + AnyCalendarKind::HijriTabularTypeIIFriday => Self::HijriTabularTypeIIFriday, |
| 69 | + // AnyCalendarKind::HijriSimulatedMecca => Self::HijriSimulatedMecca, |
| 70 | + AnyCalendarKind::HijriTabularTypeIIThursday => Self::HijriTabularTypeIIThursday, |
| 71 | + AnyCalendarKind::HijriUmmAlQura => Self::HijriUmmAlQura, |
| 72 | + AnyCalendarKind::Iso => Self::Iso, |
| 73 | + AnyCalendarKind::Japanese => Self::Japanese, |
| 74 | + AnyCalendarKind::JapaneseExtended => Self::JapaneseExtended, |
| 75 | + AnyCalendarKind::Persian => Self::Persian, |
| 76 | + AnyCalendarKind::Roc => Self::Roc, |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +macro_rules! unwrap_or_return( |
| 82 | + ($e:expr) => { |
| 83 | + { |
| 84 | + let Some(r) = $e else { |
| 85 | + return; |
| 86 | + }; |
| 87 | + r |
| 88 | + } |
| 89 | + } |
| 90 | +); |
| 91 | + |
| 92 | +fuzz_target!(|data: FuzzInput| { |
| 93 | + let calendar = AnyCalendar::new(data.cal.into()); |
| 94 | + |
| 95 | + let mut options = DateFromFieldsOptions::default(); |
| 96 | + |
| 97 | + options.overflow = if data.overflow_constrain { |
| 98 | + Some(Overflow::Constrain) |
| 99 | + } else { |
| 100 | + Some(Overflow::Reject) |
| 101 | + }; |
| 102 | + |
| 103 | + let mut fields = DateFields::default(); |
| 104 | + // Temporal only cares about validity in ±270k. We generously test outside of that. |
| 105 | + // We should error on these dates instead, or otherwise handle them: https://github.com/unicode-org/icu4x/issues/7049 |
| 106 | + fields.extended_year = Some(data.year % (i32::MAX / 16)); |
| 107 | + fields.day = Some(unwrap_or_return!(NonZeroU8::new(data.day))); |
| 108 | + match data.month_interpretation { |
| 109 | + MonthInterpretation::Ordinal => { |
| 110 | + fields.ordinal_month = Some(unwrap_or_return!(NonZeroU8::new(data.month))); |
| 111 | + } |
| 112 | + MonthInterpretation::CodeNormal => { |
| 113 | + fields.month_code = Some(unwrap_or_return!(MonthCode::new_normal(data.month))); |
| 114 | + } |
| 115 | + MonthInterpretation::CodeLeap => { |
| 116 | + fields.month_code = Some(unwrap_or_return!(MonthCode::new_leap(data.month))); |
| 117 | + } |
| 118 | + }; |
| 119 | + let _date = Date::try_from_fields(fields, options, calendar); |
| 120 | +}); |
0 commit comments