Skip to content

json+struct codec should check for a length problem with != not < #3424

@bhaller

Description

@bhaller

Making a new issue for this at the request of @jeromekelleher since #3306 was merged and closed. Tagging @petrelharp since he'll be a client of this API too, in pyslim.

Copy-paste from #3306:

Right now you have:

if ((uint64_t) metadata_length < total_length) {
    ret = tsk_trace_error(TSK_ERR_FILE_FORMAT);
    goto out;
}

Can I suggest that the test should be != rather than <? Seems like it ought to match exactly if the metadata is well-formed; and I had a subtle bug just now that had me tearing my hair out that would've been caught with a != test here. Is there a reason that you want to allow a mismatch here?

In followup discussion, @jeromekelleher wrote:

I think there's a good reason to be lenient here in allowing the length of the input buffer to be greater than the sum of the two segments - you might want to add padding for alignment. So, I think the current version is good.

I replied:

Hmm – this doesn't seem useful for alignment, since the length difference goes at the end...? For alignment, you want to have extra bytes inserted in between the JSON and the binary, which is not presently supported. (I suggested adding alignment bytes above, in fact; that would be quite a useful thing to provide in this PR!) As far as I can see, all that < does for the client is introduce the possibility of bugs. (I'm handling alignment myself in SLiM now, in fact, and the use of < here was in no way helpful for doing that; all it did was hide a bug from me.)

To expand on that a bit: any bytes added for alignment purposes have to be part of the binary blob itself, in the current design. There's no way for alignment bytes to be inserted in between the JSON and the binary, except for the client to put those bytes at the beginning of the binary blob themselves – in which case the two lengths should still be equal. It seems to me that it is never useful or correct for metadata_length > total_length to be true in the code above, so the correct test is with !=, not <. I have, in fact, implemented alignment bytes in SLiM, and the two lengths are still equal. But perhaps I'm missing the point somewhere?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions