From: Vincent Bernat Date: Tue, 29 Dec 2015 08:34:15 +0000 (+0100) Subject: client: gracefully handle NULL data in writers X-Git-Tag: 0.8.0~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5828882917f666983647638056bfb4a6c7549b72;p=thirdparty%2Flldpd.git client: gracefully handle NULL data in writers --- diff --git a/src/client/jansson_writer.c b/src/client/jansson_writer.c index 42275c2a..3d5f17c8 100644 --- a/src/client/jansson_writer.c +++ b/src/client/jansson_writer.c @@ -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 diff --git a/src/client/jsonc_writer.c b/src/client/jsonc_writer.c index 76a0f47a..c94664e5 100644 --- a/src/client/jsonc_writer.c +++ b/src/client/jsonc_writer.c @@ -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 diff --git a/src/client/kv_writer.c b/src/client/kv_writer.c index b8a87e64..2983288e 100644 --- a/src/client/kv_writer.c +++ b/src/client/kv_writer.c @@ -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); } diff --git a/src/client/text_writer.c b/src/client/text_writer.c index 68b772ac..02dbdf9a 100644 --- a/src/client/text_writer.c +++ b/src/client/text_writer.c @@ -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; } diff --git a/src/client/xml_writer.c b/src/client/xml_writer.c index 7d8bbe9b..83232f15 100644 --- a/src/client/xml_writer.c +++ b/src/client/xml_writer.c @@ -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);