]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
client: gracefully handle NULL data in writers
authorVincent Bernat <vincent@bernat.im>
Tue, 29 Dec 2015 08:34:15 +0000 (09:34 +0100)
committerVincent Bernat <vincent@bernat.im>
Tue, 29 Dec 2015 08:34:15 +0000 (09:34 +0100)
src/client/jansson_writer.c
src/client/jsonc_writer.c
src/client/kv_writer.c
src/client/text_writer.c
src/client/xml_writer.c

index 42275c2a71bb3604bb62844327441fde2a33de8e..3d5f17c8f0546f1dd3339e5a5cb861c74107aa9b 100644 (file)
@@ -67,12 +67,12 @@ jansson_attr(struct writer *w, const char *tag,
        struct json_writer_private *p = w->priv;
        struct json_element *current = TAILQ_LAST(p, json_writer_private);
        json_t *jvalue;
-       if (!strcmp(value, "yes") || !strcmp(value, "on"))
+       if (value && (!strcmp(value, "yes") || !strcmp(value, "on")))
                jvalue = json_true();
-       else if (!strcmp(value, "no") || !strcmp(value, "off"))
+       else if (value && (!strcmp(value, "no") || !strcmp(value, "off")))
                jvalue = json_false();
        else
-               jvalue = json_string(value);
+               jvalue = json_string(value?value:"");
        json_object_set_new(current->el, tag, jvalue);
 }
 
@@ -81,7 +81,8 @@ jansson_data(struct writer *w, const char *data)
 {
        struct json_writer_private *p = w->priv;
        struct json_element *current = TAILQ_LAST(p, json_writer_private);
-       json_object_set_new(current->el, "value", json_string(data));
+       json_object_set_new(current->el, "value",
+           json_string(data?data:""));
 }
 
 static void
index 76a0f47abbff621d16cd802a696d9760b7349b18..c94664e53fa46dc1edbf3a7b814a839b59dce78a 100644 (file)
@@ -66,12 +66,12 @@ jsonc_attr(struct writer *w, const char *tag,
        struct json_writer_private *p = w->priv;
        struct json_element *current = TAILQ_LAST(p, json_writer_private);
        json_object *jvalue;
-       if (!strcmp(value, "yes") || !strcmp(value, "on"))
+       if (value && (!strcmp(value, "yes") || !strcmp(value, "on")))
                jvalue = json_object_new_boolean(1);
-       else if (!strcmp(value, "no") || !strcmp(value, "off"))
+       else if (value && (!strcmp(value, "no") || !strcmp(value, "off")))
                jvalue = json_object_new_boolean(0);
        else
-               jvalue = json_object_new_string(value);
+               jvalue = json_object_new_string(value?value:"");
        json_object_object_add(current->el, tag, jvalue);
 }
 
@@ -80,7 +80,8 @@ jsonc_data(struct writer *w, const char *data)
 {
        struct json_writer_private *p = w->priv;
        struct json_element *current = TAILQ_LAST(p, json_writer_private);
-       json_object_object_add(current->el, "value", json_object_new_string(data));
+       json_object_object_add(current->el, "value",
+           json_object_new_string(data?data:""));
 }
 
 static void
index b8a87e64715362b47e56272956ce047703c456a3..2983288e0ea3d38712725d329e33c1e557c969ac 100644 (file)
@@ -56,7 +56,7 @@ kv_data(struct writer *w, const char *data)
        char *dot;
        if (!key) fatal(NULL, NULL);
        while ((dot = strchr(key, '\1')) != NULL) *dot=SEP;
-       fprintf(p->fh, "%s=%s\n", key, data);
+       fprintf(p->fh, "%s=%s\n", key, data?data:"");
        free(key);
 }
 
index 68b772acc070bb704888b5558679d347163837d3..02dbdf9a9f187863b2992c68a44f041cd0d892b6 100644 (file)
@@ -61,9 +61,9 @@ txt_attr(struct writer *w, const char *tag, const char *descr, const char *value
        struct txt_writer_private *p = w->priv;
 
        if (descr == NULL || strlen(descr) == 0) {
-               fprintf(p->fh, "%s%s", (p->attrs > 0 ? ", " : " "), value);
+               fprintf(p->fh, "%s%s", (p->attrs > 0 ? ", " : " "), value?value:"(none)");
        } else {
-               fprintf(p->fh, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr, value);
+               fprintf(p->fh, "%s%s: %s", (p->attrs > 0 ? ", " : " "), descr, value?value:"(none)");
        }
 
        p->attrs++;
@@ -73,10 +73,10 @@ static void
 txt_data(struct writer *w, const char *data) {
        struct txt_writer_private *p = w->priv;
        char *nl, *begin;
-       char *v = begin = strdup(data);
+       char *v = begin = data?strdup(data):NULL;
 
        if (v == NULL) {
-               fprintf(p->fh, " %s", data);
+               fprintf(p->fh, " %s", data?data:"(none)");
                return;
        }
 
index 7d8bbe9b33dbd62f80936a0cd7c1e07159e33a16..83232f159137a79bcefaf610c5bfe53c4f6e1e49 100644 (file)
@@ -52,14 +52,14 @@ void xml_start(struct writer * w , const char * tag, const char * descr ) {
 void xml_attr(struct writer * w, const char * tag, const char * descr, const char * value ) {
        struct xml_writer_private * p = w->priv;
 
-       if (xmlTextWriterWriteFormatAttribute(p->xw, BAD_CAST tag, "%s", value) < 0)
-               log_warnx("lldpctl", "cannot add attribute %s with value %s", tag, value);
+       if (xmlTextWriterWriteFormatAttribute(p->xw, BAD_CAST tag, "%s", value?value:"") < 0)
+               log_warnx("lldpctl", "cannot add attribute %s with value %s", tag, value?value:"(none)");
 }
 
 void xml_data(struct writer * w, const char * data) {
        struct xml_writer_private * p = w->priv;
-       if (xmlTextWriterWriteString(p->xw, BAD_CAST data) < 0 )
-               log_warnx("lldpctl", "cannot add '%s' as data to element", data);
+       if (xmlTextWriterWriteString(p->xw, BAD_CAST (data?data:"")) < 0 )
+               log_warnx("lldpctl", "cannot add '%s' as data to element", data?data:"(none)");
 }
 
 void xml_end(struct writer * w) {
@@ -81,7 +81,7 @@ void xml_finish(struct writer * w) {
        }
 
        xmlFreeTextWriter(p->xw);
-       
+
        if ( ! failed )
                xmlSaveFileEnc("-", p->doc, MY_ENCODING);