[riot-notifications] [RIOT-OS/RIOT] sys/ubjson: Do not invert bool when writing it (#11702)

Frank Hessel notifications at github.com
Fri Jun 14 19:23:32 CEST 2019


### Contribution description

The UBJSON writer inverts boolean values when writing the output. This does not seem to be the intended behavior. This PR reverts the inversion.

UBJSON in general uses a `<type-marker>[value]` syntax, and for boolean values, there are [two dedicated markers](http://ubjson.org/type-reference/value-types/#boolean) for true (`T`) and false (`F`). Those are interchanged when writing.

### Testing procedure

The following demo application can be run on `native` to examine the issue in isolation:

<details>
<summary>main.c</summary>

```c
#include <ctype.h>
#include <stdio.h>
#include "ubjson.h"

/** Callback to print the data to the console. */
int write_cb(ubjson_cookie_t *cookie, const void *buf, size_t len);

int main(void)
{
    ubjson_cookie_t cookie;
    ubjson_write_init(&cookie, write_cb);
    ubjson_open_object(&cookie);
    ubjson_write_key(&cookie, "bool_true", 9);
    ubjson_write_bool(&cookie, true);
    ubjson_write_key(&cookie, "bool_false", 10);
    ubjson_write_bool(&cookie, false);
    ubjson_close_object(&cookie);
    return 0;
}

int write_cb(ubjson_cookie_t *cookie, const void *buf, size_t len)
{
    (void)cookie;
    const uint8_t *tbuf = buf;
    for(size_t idx = 0; idx < len; idx++) {
        printf("0x%02x ", tbuf[idx]);
        putchar(isprint(tbuf[idx]) ? tbuf[idx] : '.');
        printf("\n");
    }
    return len;
}
```

</details>

<details>
<summary>Makefile</summary>

```Makefile
APPLICATION = ubjson-test-bool
BOARD ?= native
RIOTBASE ?= $(CURDIR)/../..
DEVELHELP ?= 1
QUIET ?= 1
USEMODULE += ubjson
include $(RIOTBASE)/Makefile.include
```

</details>

Before applying the fix, it will output:

```
0x7b 0x69 0x09 0x62 0x6f 0x6f 0x6c 0x5f 0x74 0x72 0x75 0x65 0x46 0x69 0x0a 0x62 0x6f 0x6f 0x6c 0x5f 0x66 0x61 0x6c 0x73 0x65 0x54 0x7d
{    i    .    b    o    o    l    _    t    r    u    e    F    i    .    b    o    o    l    _    f    a    l    s    e    T    }
```

With the fix, the values will be correct (note the different markers after `bool_true` and `bool_false`):

```
0x7b 0x69 0x09 0x62 0x6f 0x6f 0x6c 0x5f 0x74 0x72 0x75 0x65 0x54 0x69 0x0a 0x62 0x6f 0x6f 0x6c 0x5f 0x66 0x61 0x6c 0x73 0x65 0x46 0x7d
{    i    .    b    o    o    l    _    t    r    u    e    T    i    .    b    o    o    l    _    f    a    l    s    e    F    }
```

### Issues/PRs references

None.

You can view, comment on, or merge this pull request online at:

  https://github.com/RIOT-OS/RIOT/pull/11702

-- Commit Summary --

  * sys/ubjson: Do not invert bool when writing it

-- File Changes --

    M sys/ubjson/ubjson-write.c (2)

-- Patch Links --

https://github.com/RIOT-OS/RIOT/pull/11702.patch
https://github.com/RIOT-OS/RIOT/pull/11702.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/11702
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190614/c3ce8afd/attachment.html>


More information about the notifications mailing list