From: Ruben Kerkhof Date: Sun, 16 Dec 2018 14:11:02 +0000 (+0100) Subject: Treewide: do NUL-termination correctly X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=a96e1c4a5e2c0244607b9da71175c2a12a4451fd;p=collectd.git Treewide: do NUL-termination correctly This is purely cosmetic but still, it sets a good example --- diff --git a/src/ceph.c b/src/ceph.c index c112af06..26cf2155 100644 --- a/src/ceph.c +++ b/src/ceph.c @@ -251,7 +251,7 @@ static int ceph_cb_boolean(void *ctx, int bool_val) { return CEPH_CB_CONTINUE; } if (dest_size > dest_len) { \ sstrncpy((dest) + dest_len, (src), dest_size - dest_len); \ } \ - (dest)[dest_size - 1] = 0; \ + (dest)[dest_size - 1] = '\0'; \ } while (0) static int ceph_cb_number(void *ctx, const char *number_val, @@ -350,7 +350,7 @@ static int ceph_cb_map_key(void *ctx, const unsigned char *key, } memmove(state->key, key, sz - 1); - state->key[sz - 1] = 0; + state->key[sz - 1] = '\0'; return CEPH_CB_CONTINUE; } diff --git a/src/cgroups.c b/src/cgroups.c index 7f24d12e..9304216d 100644 --- a/src/cgroups.c +++ b/src/cgroups.c @@ -112,7 +112,7 @@ static int read_cpuacct_procs(const char *dirname, char const *cgroup_name, /* Strip colon off the first column, if found */ if (key[key_len - 1] == ':') - key[key_len - 1] = 0; + key[key_len - 1] = '\0'; status = parse_value(fields[1], &value, DS_TYPE_DERIVE); if (status != 0) diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index 33580f9e..06298494 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -527,7 +527,7 @@ static int do_check(lcc_connection_t *connection) { int status; snprintf(ident_str, sizeof(ident_str), "%s/%s", hostname_g, value_string_g); - ident_str[sizeof(ident_str) - 1] = 0; + ident_str[sizeof(ident_str) - 1] = '\0'; status = lcc_string_to_identifier(connection, &ident, ident_str); if (status != 0) { @@ -652,7 +652,7 @@ int main(int argc, char **argv) { } snprintf(address, sizeof(address), "unix:%s", socket_file_g); - address[sizeof(address) - 1] = 0; + address[sizeof(address) - 1] = '\0'; connection = NULL; status = lcc_connect(address, &connection); diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 2d83bbfe..5210a221 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -196,7 +196,7 @@ static lcc_value_list_t *create_value_list(void) /* {{{ */ strncpy(vl->identifier.type, (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive", sizeof(vl->identifier.type)); - vl->identifier.type[sizeof(vl->identifier.type) - 1] = 0; + vl->identifier.type[sizeof(vl->identifier.type) - 1] = '\0'; snprintf(vl->identifier.type_instance, sizeof(vl->identifier.type_instance), "ti%li", random()); diff --git a/src/curl_json.c b/src/curl_json.c index dedfed05..3b2fffe0 100644 --- a/src/curl_json.c +++ b/src/curl_json.c @@ -236,7 +236,7 @@ static int cj_cb_number(void *ctx, const char *number, yajl_len_t number_len) { /* Create a null-terminated version of the string. */ char buffer[number_len + 1]; memcpy(buffer, number, number_len); - buffer[sizeof(buffer) - 1] = 0; + buffer[sizeof(buffer) - 1] = '\0'; if (db->state[db->depth].entry == NULL || db->state[db->depth].entry->type != KEY) { @@ -272,7 +272,7 @@ static int cj_cb_map_key(void *ctx, unsigned char const *in_name, char name[in_name_len + 1]; memmove(name, in_name, in_name_len); - name[sizeof(name) - 1] = 0; + name[sizeof(name) - 1] = '\0'; if (cj_load_key(ctx, name) != 0) return CJ_CB_ABORT; diff --git a/src/daemon/common.c b/src/daemon/common.c index 99f48caf..1775573a 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -442,7 +442,7 @@ int strunescape(char *buf, size_t buf_len) { /* Move everything after the position one position to the left. * Add a null-byte as last character in the buffer. */ memmove(buf + i + 1, buf + i + 2, buf_len - i - 2); - buf[buf_len - 1] = 0; + buf[buf_len - 1] = '\0'; } return 0; } /* int strunescape */ @@ -1013,7 +1013,7 @@ int parse_value(const char *value_orig, value_t *ret_value, int ds_type) { value_len = strlen(value); while ((value_len > 0) && isspace((int)value[value_len - 1])) { - value[value_len - 1] = 0; + value[value_len - 1] = '\0'; value_len--; } diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 8750bef8..95cb32fc 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -1064,7 +1064,7 @@ int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */ } /* }}} int cf_util_get_string */ /* Assures the config option is a string and copies it to the provided buffer. - * Assures null-termination. */ + * Assures NUL-termination. */ int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */ size_t buffer_size) { if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1)) @@ -1076,7 +1076,7 @@ int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */ } strncpy(buffer, ci->values[0].value.string, buffer_size); - buffer[buffer_size - 1] = 0; + buffer[buffer_size - 1] = '\0'; return 0; } /* }}} int cf_util_get_string_buffer */ diff --git a/src/ipmi.c b/src/ipmi.c index 15909c2b..58dfb411 100644 --- a/src/ipmi.c +++ b/src/ipmi.c @@ -113,7 +113,7 @@ static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) { if (errbuf[0] == 0) { snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status); } - errbuf[sizeof(errbuf) - 1] = 0; + errbuf[sizeof(errbuf) - 1] = '\0'; ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf); } /* void c_ipmi_error */ @@ -310,7 +310,7 @@ static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) { return; ipmi_sensor_get_name(sensor, temp, sizeof(temp)); - temp[sizeof(temp) - 1] = 0; + temp[sizeof(temp) - 1] = '\0'; if (entity_id_string != NULL && strlen(temp)) snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string); diff --git a/src/irq.c b/src/irq.c index aa6ac04b..eeea058a 100644 --- a/src/irq.c +++ b/src/irq.c @@ -138,7 +138,7 @@ static int irq_read(void) { if (irq_name_len == 4 && (strncmp(irq_name, "FIQ:", 4) == 0)) continue; - irq_name[irq_name_len - 1] = 0; + irq_name[irq_name_len - 1] = '\0'; irq_name_len--; irq_value = 0; diff --git a/src/libcollectdclient/client.c b/src/libcollectdclient/client.c index df6fd960..69f97640 100644 --- a/src/libcollectdclient/client.c +++ b/src/libcollectdclient/client.c @@ -80,22 +80,22 @@ * is very useful to add formatted stuff to the end of a buffer. */ #define SSTRCPY(d, s) \ do { \ - strncpy((d), (s), sizeof(d)); \ - (d)[sizeof(d) - 1] = 0; \ + strncpy((d), (s), sizeof(d) - 1); \ + (d)[sizeof(d) - 1] = '\0'; \ } while (0) #define SSTRCAT(d, s) \ do { \ size_t _l = strlen(d); \ strncpy((d) + _l, (s), sizeof(d) - _l); \ - (d)[sizeof(d) - 1] = 0; \ + (d)[sizeof(d) - 1] = '\0'; \ } while (0) #define SSTRCATF(d, ...) \ do { \ char _b[sizeof(d)]; \ snprintf(_b, sizeof(_b), __VA_ARGS__); \ - _b[sizeof(_b) - 1] = 0; \ + _b[sizeof(_b) - 1] = '\0'; \ SSTRCAT((d), _b); \ } while (0) @@ -172,7 +172,7 @@ static char *sstrerror(int errnum, char *buf, size_t buflen) { } #endif /* STRERROR_R_CHAR_P */ - buf[buflen - 1] = 0; + buf[buflen - 1] = '\0'; return buf; } /* char *sstrerror */ @@ -183,7 +183,7 @@ static int lcc_set_errno(lcc_connection_t *c, int err) /* {{{ */ return -1; sstrerror(err, c->errbuf, sizeof(c->errbuf)); - c->errbuf[sizeof(c->errbuf) - 1] = 0; + c->errbuf[sizeof(c->errbuf) - 1] = '\0'; return 0; } /* }}} int lcc_set_errno */ @@ -244,7 +244,7 @@ static void lcc_chomp(char *str) /* {{{ */ while (str_len > 0) { if (str[str_len - 1] >= 32) break; - str[str_len - 1] = 0; + str[str_len - 1] = '\0'; str_len--; } } /* }}} void lcc_chomp */ @@ -308,7 +308,7 @@ static int lcc_receive(lcc_connection_t *c, /* {{{ */ /* Now copy the message. */ strncpy(res.message, ptr, sizeof(res.message)); - res.message[sizeof(res.message) - 1] = 0; + res.message[sizeof(res.message) - 1] = '\0'; /* Error or no lines follow: We're done. */ if (res.status <= 0) { @@ -624,7 +624,7 @@ int lcc_getval(lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ snprintf(command, sizeof(command), "GETVAL %s", lcc_strescape(ident_esc, ident_str, sizeof(ident_esc))); - command[sizeof(command) - 1] = 0; + command[sizeof(command) - 1] = '\0'; /* Send talk to the daemon.. */ status = lcc_sendreceive(c, command, &res); @@ -930,7 +930,7 @@ int lcc_identifier_to_string(lcc_connection_t *c, /* {{{ */ ident->type_instance); } - string[string_size - 1] = 0; + string[string_size - 1] = '\0'; return 0; } /* }}} int lcc_identifier_to_string */ diff --git a/src/match_regex.c b/src/match_regex.c index ed1e3295..20445cc1 100644 --- a/src/match_regex.c +++ b/src/match_regex.c @@ -151,7 +151,7 @@ static int mr_add_regex(mr_regex_t **re_head, const char *re_str, /* {{{ */ if (status != 0) { char errmsg[1024]; regerror(status, &re->re, errmsg, sizeof(errmsg)); - errmsg[sizeof(errmsg) - 1] = 0; + errmsg[sizeof(errmsg) - 1] = '\0'; log_err("Compiling regex `%s' for `%s' failed: %s.", re->re_str, option, errmsg); sfree(re->re_str); diff --git a/src/netapp.c b/src/netapp.c index 62600ca2..0c1fe31d 100644 --- a/src/netapp.c +++ b/src/netapp.c @@ -2974,7 +2974,7 @@ static int cna_init(void) /* {{{ */ char err[256] = {0}; if (!na_startup(err, sizeof(err))) { - err[sizeof(err) - 1] = 0; + err[sizeof(err) - 1] = '\0'; ERROR("netapp plugin: Error initializing netapp API: %s", err); return 1; } diff --git a/src/network.c b/src/network.c index 64c6d24b..af23d485 100644 --- a/src/network.c +++ b/src/network.c @@ -403,7 +403,7 @@ static int network_dispatch_values(value_list_t *vl, /* {{{ */ #if COLLECT_DEBUG char name[6 * DATA_MAX_NAME_LEN]; FORMAT_VL(name, sizeof(name), vl); - name[sizeof(name) - 1] = 0; + name[sizeof(name) - 1] = '\0'; DEBUG("network plugin: network_dispatch_values: " "NOT dispatching %s.", name); @@ -2640,7 +2640,7 @@ static int network_write(const data_set_t *ds, const value_list_t *vl, #if COLLECT_DEBUG char name[6 * DATA_MAX_NAME_LEN]; FORMAT_VL(name, sizeof(name), vl); - name[sizeof(name) - 1] = 0; + name[sizeof(name) - 1] = '\0'; DEBUG("network plugin: network_write: " "NOT sending %s.", name); diff --git a/src/onewire.c b/src/onewire.c index 49c6aa37..575a6820 100644 --- a/src/onewire.c +++ b/src/onewire.c @@ -299,7 +299,7 @@ static int cow_read_values(const char *path, const char *name, snprintf(file, sizeof(file), "%s/%s", path, family_info->features[i].filename); - file[sizeof(file) - 1] = 0; + file[sizeof(file) - 1] = '\0'; buffer = NULL; buffer_size = 0; diff --git a/src/oracle.c b/src/oracle.c index 1982aeef..f54b285a 100644 --- a/src/oracle.c +++ b/src/oracle.c @@ -107,7 +107,7 @@ static void o_report_error(const char *where, /* {{{ */ status = OCIErrorGet(eh, (ub4)record_number, /* sqlstate = */ NULL, &error_code, (text *)&buffer[0], (ub4)sizeof(buffer), OCI_HTYPE_ERROR); - buffer[sizeof(buffer) - 1] = 0; + buffer[sizeof(buffer) - 1] = '\0'; if (status == OCI_NO_DATA) return; diff --git a/src/powerdns.c b/src/powerdns.c index 644d0ae7..b2cebbf8 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -469,7 +469,7 @@ static int powerdns_get_data_dgram(list_item_t *item, char **ret_buffer) { } memcpy(buffer, temp, buffer_size - 1); - buffer[buffer_size - 1] = 0; + buffer[buffer_size - 1] = '\0'; *ret_buffer = buffer; return 0; diff --git a/src/processes.c b/src/processes.c index aa7cfa39..ce4cc652 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1577,7 +1577,6 @@ static char *ps_get_cmdline(long pid, return NULL; } - info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0; sstrncpy(buffer, info.pr_psargs, buffer_size); return buffer; diff --git a/src/python.c b/src/python.c index 64db6985..2c62146d 100644 --- a/src/python.c +++ b/src/python.c @@ -367,7 +367,7 @@ void cpy_log_exception(const char *context) { continue; if (cpy[strlen(cpy) - 1] == '\n') - cpy[strlen(cpy) - 1] = 0; + cpy[strlen(cpy) - 1] = '\0'; Py_BEGIN_ALLOW_THREADS; ERROR("%s", cpy); diff --git a/src/rrdtool.c b/src/rrdtool.c index f6290d73..605a16cc 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -569,7 +569,7 @@ static int rrd_cache_flush_identifier(cdtime_t timeout, snprintf(key, sizeof(key), "%s.rrd", identifier); else snprintf(key, sizeof(key), "%s/%s.rrd", datadir, identifier); - key[sizeof(key) - 1] = 0; + key[sizeof(key) - 1] = '\0'; status = c_avl_get(cache, key, (void *)&rc); if (status != 0) { diff --git a/src/serial.c b/src/serial.c index 8bbd94c0..2b865370 100644 --- a/src/serial.c +++ b/src/serial.c @@ -79,7 +79,7 @@ static int serial_read(void) { continue; if (fields[0][len - 1] != ':') continue; - fields[0][len - 1] = 0; + fields[0][len - 1] = '\0'; for (int i = 1; i < numfields; i++) { len = strlen(fields[i]); diff --git a/src/snmp.c b/src/snmp.c index af26fbd8..fb1b83c3 100644 --- a/src/snmp.c +++ b/src/snmp.c @@ -1146,7 +1146,7 @@ static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */ if (((size_t)status) >= buffer_free) /* truncated */ { - dst[dst_size - 1] = 0; + dst[dst_size - 1] = '\0'; return ENOMEM; } else /* if (status < buffer_free) */ { @@ -1193,7 +1193,7 @@ static int csnmp_strvbcopy(char *dst, /* {{{ */ dst[i] = src[i]; } dst[num_chars] = 0; - dst[dst_size - 1] = 0; + dst[dst_size - 1] = '\0'; if (dst_size <= vb->val_len) return ENOMEM; diff --git a/src/tail_csv.c b/src/tail_csv.c index 48f3d743..4c589716 100644 --- a/src/tail_csv.c +++ b/src/tail_csv.c @@ -143,7 +143,7 @@ static int tcsv_read_buffer(instance_definition_t *id, char *buffer, while (buffer_size > 0) { if ((buffer[buffer_size - 1] == '\n') || (buffer[buffer_size - 1] == '\r')) { - buffer[buffer_size - 1] = 0; + buffer[buffer_size - 1] = '\0'; buffer_size--; } else { break; diff --git a/src/teamspeak2.c b/src/teamspeak2.c index 6d0cdbb8..567e5fb8 100644 --- a/src/teamspeak2.c +++ b/src/teamspeak2.c @@ -255,7 +255,7 @@ static int tss2_get_socket(FILE **ret_read_fh, FILE **ret_write_fh) { config_host ? config_host : DEFAULT_HOST, config_port ? config_port : DEFAULT_PORT); } - buffer[sizeof(buffer) - 1] = 0; + buffer[sizeof(buffer) - 1] = '\0'; if (memcmp("[TS]\r\n", buffer, 6) != 0) { ERROR("teamspeak2 plugin: Unexpected response when connecting " @@ -309,7 +309,7 @@ static int tss2_receive_line(FILE *fh, char *buffer, int buffer_size) { return -1; } - buffer[buffer_size - 1] = 0; + buffer[buffer_size - 1] = '\0'; return 0; } /* int tss2_receive_line */ @@ -337,7 +337,7 @@ static int tss2_select_vserver(FILE *read_fh, FILE *write_fh, ERROR("teamspeak2 plugin: tss2_receive_line failed."); return -1; } - response[sizeof(response) - 1] = 0; + response[sizeof(response) - 1] = '\0'; /* Check answer */ if ((strncasecmp("OK", response, 2) == 0) && @@ -379,7 +379,7 @@ static int tss2_vserver_gapl(FILE *read_fh, FILE *write_fh, ERROR("teamspeak2 plugin: tss2_receive_line failed."); return -1; } - buffer[sizeof(buffer) - 1] = 0; + buffer[sizeof(buffer) - 1] = '\0'; if (strncmp("average_packet_loss=", buffer, strlen("average_packet_loss=")) == 0) { diff --git a/src/utils_db_query.c b/src/utils_db_query.c index 81ad8b5b..73c1b45e 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -226,13 +226,13 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ status); return status; } - tmp[sizeof(tmp) - 1] = 0; + tmp[sizeof(tmp) - 1] = '\0'; snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s", r->instance_prefix, tmp); } } - vl.type_instance[sizeof(vl.type_instance) - 1] = 0; + vl.type_instance[sizeof(vl.type_instance) - 1] = '\0'; /* }}} */ /* Annotate meta data. {{{ */ diff --git a/src/utils_fbhash.c b/src/utils_fbhash.c index 366b44b8..07134635 100644 --- a/src/utils_fbhash.c +++ b/src/utils_fbhash.c @@ -102,7 +102,7 @@ static int fbh_read_file(fbhash_t *h) /* {{{ */ char *key_copy; char *value_copy; - buffer[sizeof(buffer) - 1] = 0; + buffer[sizeof(buffer) - 1] = '\0'; len = strlen(buffer); /* Remove trailing newline characters. */ diff --git a/src/utils_format_json.c b/src/utils_format_json.c index 49aa2299..25cbb0a7 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -58,7 +58,7 @@ static int json_escape_string(char *buffer, size_t buffer_size, /* {{{ */ #define BUFFER_ADD(c) \ do { \ if (dst_pos >= (buffer_size - 1)) { \ - buffer[buffer_size - 1] = 0; \ + buffer[buffer_size - 1] = '\0'; \ return -ENOMEM; \ } \ buffer[dst_pos] = (c); \ diff --git a/src/utils_format_kairosdb.c b/src/utils_format_kairosdb.c index 49989062..d957bc80 100644 --- a/src/utils_format_kairosdb.c +++ b/src/utils_format_kairosdb.c @@ -69,7 +69,7 @@ static int kairosdb_escape_string(char *buffer, size_t buffer_size, /* {{{ */ #define BUFFER_ADD(c) \ do { \ if (dst_pos >= (buffer_size - 1)) { \ - buffer[buffer_size - 1] = 0; \ + buffer[buffer_size - 1] = '\0'; \ return -ENOMEM; \ } \ buffer[dst_pos] = (c); \ diff --git a/src/utils_ignorelist.c b/src/utils_ignorelist.c index 29b2af21..b3851026 100644 --- a/src/utils_ignorelist.c +++ b/src/utils_ignorelist.c @@ -267,7 +267,7 @@ int ignorelist_add(ignorelist_t *il, const char *entry) { return ENOMEM; /* trim trailing slash */ - copy[strlen(copy) - 1] = 0; + copy[strlen(copy) - 1] = '\0'; status = ignorelist_append_regex(il, copy); sfree(copy); diff --git a/src/utils_tail.c b/src/utils_tail.c index 55a32879..365bf558 100644 --- a/src/utils_tail.c +++ b/src/utils_tail.c @@ -142,7 +142,7 @@ int cu_tail_readline(cu_tail_t *obj, char *buf, int buflen) { * be fine and we can return. */ clearerr(obj->fh); if (fgets(buf, buflen, obj->fh) != NULL) { - buf[buflen - 1] = 0; + buf[buflen - 1] = '\0'; return 0; } @@ -168,7 +168,7 @@ int cu_tail_readline(cu_tail_t *obj, char *buf, int buflen) { /* If we get here: file was re-opened and there may be more to read.. Let's * try again. */ if (fgets(buf, buflen, obj->fh) != NULL) { - buf[buflen - 1] = 0; + buf[buflen - 1] = '\0'; return 0; }