Dict parsing question

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Fri Dec 15 00:17:49 UTC 2023


On Thu, 14 Dec 2023 18:34:04 -0500, Robert Middleton wrote:

> I was under the assumption that the next dict entry should start on
> the 8-byte boundary, but it appears to be starting on a 4-byte
> boundary with the length of the string.

From the spec
<https://dbus.freedesktop.org/doc/dbus-specification.html>:

    The string-like types (STRING, OBJECT_PATH and SIGNATURE) are all
    marshalled as a fixed-length unsigned integer n giving the length
    of the variable part, followed by n nonzero bytes of UTF-8 text,
    followed by a single zero (nul) byte which is not considered to be
    part of the text. The alignment of the string-like type is the same
    as the alignment of n: any padding required for n appears
    immediately before n itself. There is never any alignment padding
    between n and the string text, or between the string text and the
    trailing nul. The alignment padding for the next value in the
    message (if there is one) starts after the trailing nul.

    For the STRING and OBJECT_PATH types, n is encoded in 4 bytes (a
    UINT32), leading to 4-byte alignment.

And further along:

    Structs and dict entries are marshalled in the same way as their
    contents, but their alignment is always to an 8-byte boundary, even
    if their contents would normally be less strictly aligned.

So the beginning of a struct or dict is 8-byte-aligned, but its
individual entries are not necessarily so.


More information about the dbus mailing list