Add guard for potential empty labels from bibliography (#5776)

This commit is contained in:
Said Aroua 2025-05-25 15:50:22 +02:00
parent d90b5623d8
commit 43c3acdc4e
3 changed files with 12 additions and 2 deletions

View File

@ -54,7 +54,9 @@ pub struct Label(PicoStr);
impl Label { impl Label {
/// Creates a label from an interned string. /// Creates a label from an interned string.
/// Callers need to ensure the given string is not empty.
pub fn new(name: PicoStr) -> Self { pub fn new(name: PicoStr) -> Self {
debug_assert!(name != PicoStr::EMPTY);
Self(name) Self(name)
} }

View File

@ -313,12 +313,17 @@ impl Bibliography {
for (source, data) in sources.0.iter().zip(data) { for (source, data) in sources.0.iter().zip(data) {
let library = decode_library(source, data)?; let library = decode_library(source, data)?;
for entry in library { for entry in library {
match map.entry(Label::new(PicoStr::intern(entry.key()))) { let key = entry.key();
if key.is_empty() {
bail!("empty bibliography key found");
}
match map.entry(Label::new(PicoStr::intern(key))) {
indexmap::map::Entry::Vacant(vacant) => { indexmap::map::Entry::Vacant(vacant) => {
vacant.insert(entry); vacant.insert(entry);
} }
indexmap::map::Entry::Occupied(_) => { indexmap::map::Entry::Occupied(_) => {
duplicates.push(entry.key().into()); duplicates.push(key.into());
} }
} }
} }

View File

@ -38,6 +38,9 @@ struct Interner {
pub struct PicoStr(NonZeroU64); pub struct PicoStr(NonZeroU64);
impl PicoStr { impl PicoStr {
/// Empty string as PicoStr
pub const EMPTY: PicoStr = PicoStr::constant("");
/// Intern a string at runtime. /// Intern a string at runtime.
pub fn intern(string: &str) -> PicoStr { pub fn intern(string: &str) -> PicoStr {
// Try to use bitcode or exception representations. // Try to use bitcode or exception representations.