rust-vmm / vm-fdt

Apache License 2.0
14 stars 16 forks source link

begin_node does not enforce limits defined in the specification #32

Closed andreeaflorescu closed 3 years ago

andreeaflorescu commented 3 years ago

The FDT specification describes a string length of e.g. the begin_node of 1 to 31 chars and enforces a strict character limitation described in https://devicetree-specification.readthedocs.io/en/stable/devicetree-basics.html#node-name-requirements.

Enforcing the character limitation would mean adding a dependency on regex, which is undesirable. We could instead enforce the size limitation, and document the character limitation as something that is not currently validated.

@danielverkamp what do you think?

danielverkamp commented 3 years ago

I think we can check the valid chars without adding a regex dependency - it's a little verbose, but something like this should work:

const NODE_NAME_MAX_CHARS: usize = 31;
const NODE_NAME_VALID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,._+-";

fn name_ok(name: &str) -> bool {
    if name.is_empty() || name.len() > NODE_NAME_MAX_CHARS {
        return false;
    }
    !name.contains(|c: char| !NODE_NAME_VALID_CHARS.contains(c))
}

I can put together a PR if you think this approach looks OK.

danielverkamp commented 3 years ago

I tweaked the checks some more using matches and added some checks for the @unit-address part in node names.