dbus_message_set/get_data usage

Simon McVittie simon.mcvittie at collabora.co.uk
Thu Dec 12 07:10:17 PST 2013


On 12/12/13 13:18, Brosseau, Vincent wrote:
> You say that char[m][n] is not the same type as char*[n]. Why ?
> Arrays are pointers ([] = *), and addresses are pointers (& = *).

Arrays are similar to pointers, but not the same.
See http://c-faq.com/aryptr/

> As far as I know, in my case &param.value[index] is like char** (isn't it ?)

No; if it was, your code wouldn't be crashing.

> dbus_message_iter_append_basic() knows that it's a string thanks
> to its second argument "DBUS_TYPE_STRING", then it knows that there's
> something else after the "h" (up to \0).

Suppose the "h" in "hello" is at memory address 0x88888888 and your
machine is 32-bit (sizeof(char *) == 4, or 8 hex digits).
dbus_message_iter_append_basic(., DBUS_TYPE_STRING, .) wants to receive
the address of a piece of memory containing 0x88888888 as its third
argument.

&param.value[index] is the address of the array param.value[index],
which is the address of the first element of the array, which is
0x88888888. dbus_message_iter_append_basic() reads the bytes from
0x88888888 to 0x8888888b, and they are ('h','e','l','l'), which is
probably not a valid pointer, leading to a segfault when it dereferences
that "pointer".

If you passed param.value[index] as a function argument, the numeric
value pushed onto the stack[1] would *also* be 0x88888888.

If you have a char ** temporary variable "tmp" (let's say its address is
0x44444444), and you say "tmp = &param.value[index]", that sets the
bytes (0x44444444 ... 0x44444447) to 0x88888888; then calling
dbus_message_iter_append_basic (., DBUS_TYPE_STRING, tmp) puts
0x44444444 on the stack[1]. That's the address of a piece of memory
containing 0x88888888, as desired.

    S

[1] or maybe in a CPU register, but the principle's the same



More information about the dbus mailing list