Re: [PATCH v3 2/7] gpu: nova-core: add TLV parser for firmware files

Timur Tabi <[email protected]>
Newsgroups dev.linux.lists.nova-gpu,dev.linux.lists.driver-core,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Tue, 2026-07-07 at 14:13 +0900, Alexandre Courbot wrote:
> > Returning error on length==0 simplifies a lot of code on the caller side.
> 
> Can you give an example of such a simplification? I'm fine if there is a
> benefit to doing so, but would like to understand what we gain.

Well, maybe not "a lot" of code, but in general, it eliminates the need to test for empty slices
every time.  If get_bytes() returns success, you know that you actually do have some bytes.

For example:

        let sig_bytes = self.get_bytes(b"SIGN")?;

        // Ensure that sig_bytes can be divided evenly into chunks.
        if sig_bytes.len() % num_sigs != 0 {
            return Err(EINVAL);
        }

        // num_sigs cannot be 0, and sig_bytes cannot be empty, so this cannot panic.
        let sig_size = sig_bytes.len() / num_sigs;

sig_bytes.len() cannot be 0, so we know that sig_size cannot be 0, and therefore

        sig_bytes.chunks_exact(sig_size).nth(index).ok_or(EINVAL)

chunks_exact() cannot fail.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.