Code

curl_json plugin: Skip unexpected non-map values.
authorFlorian Forster <octo@collectd.org>
Tue, 13 Sep 2016 08:44:46 +0000 (10:44 +0200)
committerFlorian Forster <octo@collectd.org>
Tue, 13 Sep 2016 08:45:01 +0000 (10:45 +0200)
commitba52f94929822b1968f05cfbb37985af2e0b83a4
treed8fc9c0c7c6c19e54249ce2ad92d50ee0852a2b7
parent276d1e1bdfc3e02431bb05c06da7a16f80fa7e47
curl_json plugin: Skip unexpected non-map values.

Assume, for example, the config `Key "*/foo"`. This config expects JSON
in the form:

    {
      "bar": {
        "foo": 1337
      }
    }

If the available JSON is instead:

    {
      "error_code": 0,
      "bar": {
        "foo": 1337
      }
    }

the code will take a look at the zero associated with "error_code" and
determine that a map (with key "foo") is expected instead. Previously
the code would continue, eventually calling `cj_get_type()` which
expects that `key->type` is a valid pointer, resulting in a segmentation
fault.

This patch does three things to ensure that this segmentation fault does
not happen again:

1.  `cj_get_type()` checks its argument to make sure it is valid before
    dereferencing any pointers.
2.  In case a non-map is found when a map is expected, the code will
    return instead of limping on.
3.  After calling `cj_cb_inc_array_index()`, which may update the key,
    make sure that it actually did and that key is valid now.

Fixes: #1896
src/curl_json.c