derive: don't suppress fatal errors in integer parsing

This commit is contained in:
René Kijewski 2025-05-19 11:38:52 +02:00
parent 3e5562dc7c
commit 359da68fa0
3 changed files with 32 additions and 7 deletions

View File

@ -5,6 +5,7 @@ use std::path::Path;
use console::style;
use prettyplease::unparse;
use proc_macro2::TokenStream;
use quote::quote;
use similar::{Algorithm, ChangeTag, TextDiffConfig};
@ -19,6 +20,12 @@ fn build_template(ast: &syn::DeriveInput) -> Result<String, crate::CompileError>
Ok(buf.into_string())
}
fn import_askama() -> TokenStream {
quote! {
extern crate askama;
}
}
// This function makes it much easier to compare expected code by adding the wrapping around
// the code we want to check.
#[track_caller]
@ -36,7 +43,7 @@ fn compare_ex(
) {
let generated = jinja_to_rust(jinja, fields, prefix).unwrap();
let expected: proc_macro2::TokenStream = expected.parse().unwrap();
let expected: TokenStream = expected.parse().unwrap();
let expected: syn::File = syn::parse_quote! {
impl askama::Template for Foo {
fn render_into_with_values<AskamaW>(
@ -1179,11 +1186,7 @@ fn test_generated_with_error() {
#[template(ext = "txt", source = "test {#")]
struct HelloWorld;
};
let ts = crate::derive_template(ts, || {
quote! {
extern crate askama;
}
});
let ts = crate::derive_template(ts, import_askama);
let _: syn::File = syn::parse2(ts).unwrap();
}
@ -1208,3 +1211,17 @@ fn test_filter_with_path() {
3,
);
}
#[test]
fn fuzzed_0b85() -> Result<(), syn::Error> {
let input = quote! {
#[template(
ext = "",
source = "\u{c}{{vSelf&&h<6-0b85%04540736.66609.500804540736.660<c7~}}2/3\0{w66hi%e<a}}"
)]
struct a {}
};
let output = crate::derive_template(input, import_askama);
let _: syn::File = syn::parse2(output)?;
Ok(())
}

View File

@ -476,7 +476,7 @@ fn num_lit<'a>(i: &mut &'a str) -> ParseResult<'a, Num<'a>> {
}
};
let num = if let Ok(Some(num)) = opt(int_with_base.take()).parse_next(i) {
let num = if let Some(num) = opt(int_with_base.take()).parse_next(i)? {
let suffix =
opt(|i: &mut _| num_lit_suffix("integer", INTEGER_TYPES, start, i)).parse_next(i)?;
Num::Int(num, suffix)

View File

@ -1415,3 +1415,11 @@ fn underscore_is_an_identifier() {
assert_eq!(result.unwrap(), "_");
assert_eq!(input, "");
}
#[test]
fn there_is_no_digit_two_in_a_binary_integer() {
let syntax = Syntax::default();
assert!(Ast::from_str("{{ 0b2 }}", None, &syntax).is_err());
assert!(Ast::from_str("{{ 0o9 }}", None, &syntax).is_err());
assert!(Ast::from_str("{{ 0xg }}", None, &syntax).is_err());
}