jrgerber/smbios-lib

Refactor UndefinedStructTable parsing logic

jrgerber opened this issue · 0 comments

The UndefinedStructTable takes a vector of raw SMBIOS table data and parses it into chunks which represent the yet to be defined SMBIOS structures. The logic that does this is contained within:
impl From<Vec<u8>> for UndefinedStructTable

The logic is buggy, difficult to read, and needs a refactor (make it at least look more like functional programming). For example the logic that searches for a double null has a bug which causes it to sometimes exceed the end of the array.

            // next_index is pointing at the start of the string area.
            // The string area is terminated with \0\0.  If no strings exist then its contents is \0\0.
            // Search for \0\0 and point at the byte immediately after it.  That point is either the start of the
            // next structure header or one byte beyond the end of "data".
            let mut a: bool;
            let mut b = true;
            loop {
                if next_index >= len {
                    break;
                }
                a = data[next_index] != 0;
                next_index = next_index + 1;
                if a || b {
                    b = data[next_index] != 0;
                    next_index = next_index + 1;
                }
                if !(a || b) {
                    break;
                }
            }

Further, the logic that guarantees there is a double null at the end of the array is in a loop and does not need to be:

            || data[len - 2] != 0 // 2nd to last byte should be zero and it is not
            || data[len - 1] != 0