[blob.c] What is BLOB_COOKIE used for?

Hi!

I can't understand how the constant BLOB_COOKIE is used (c.f. blob.c).

static struct blob_attr *
blob_add(struct blob_buf *buf, struct blob_attr *pos, int id, int payload)
{
	int offset = attr_to_offset(buf, pos);
	int required = (offset - BLOB_COOKIE + sizeof(struct blob_attr) + payload) - buf->buflen;
    ...
}
static inline int
attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
{
	return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
}

It seems to me that the function attr_to_offset calculates the offset between the start of the buffer and the position in the buffer of the new blob attribute. For some unknown reason, the integer constant BLOB_COOKIE is added to the returned value of attr_to_offset (offset). However, BLOB_COOKIE is again subtracted from the next value (offset - BLOB_COOKIE). So it's as if we never added the value of BLOB_COOKIE, is it?

now i am learning ubus code and also don't understand this magic
let's ask the author

Hi @g41797!

I have trouble to read the source code.

It seems to me that BLOB_COOKIE is really used when several blobs are nested (see blob_nest_start and blob_nest_end).

my bet - BLOB_COOKIE is used just for disabling possibility to use returned value as real offset

i saved doxygen generated files of ubus libs and exes in my repo
download zip file
extract it

  • dgen/ubusdgen/html/index.html is root of generated ubus documentation
  • _notes.txt in the root folder contains lot of ubus related links