Skip to content

Commit

Permalink
Fix issues #439, #303 (#545)
Browse files Browse the repository at this point in the history
* Fix issues #439 , #303

* core.cc: Add missing header file

* core.cc: Check if we build for x11 and add the locks functions

* read_tcpip.cc: Fix for #306

* read_tcpip.cc: Missing string format specifier for "snprintf()"

* read_tcpip.cc: Wrong signed int format specifier for "snprintf()"

* Missing many string format specifiers to "snprintf()"
  • Loading branch information
su8 authored and brndnmtthws committed Aug 2, 2018
1 parent 71cfbff commit 7bca0ca
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/algebra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ enum arg_type get_arg_type(const char *arg) {
p++;
}
while (p <= e) {
if (isdigit(*p) == 0) {
if (isdigit((unsigned char)*p) == 0) {
break;
}
p++;
Expand All @@ -158,7 +158,7 @@ enum arg_type get_arg_type(const char *arg) {
if (*p == '.') {
p++;
while (p <= e) {
if (isdigit(*p) == 0) {
if (isdigit((unsigned char)*p) == 0) {
return ARG_BAD;
}
p++;
Expand Down
14 changes: 7 additions & 7 deletions src/bsdapm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ void print_apm_adapter(struct text_object *obj, char *p, int p_max_size) {

fd = open(APMDEV, O_RDONLY);
if (fd < 0) {
snprintf(p, p_max_size, "ERR");
snprintf(p, p_max_size, "%s", "ERR");
return;
}

if (apm_getinfo(fd, &a_info) != 0) {
close(fd);
snprintf(p, p_max_size, "ERR");
snprintf(p, p_max_size, "%s", "ERR");
return;
}
close(fd);
Expand Down Expand Up @@ -123,13 +123,13 @@ void print_apm_battery_life(struct text_object *obj, char *p, int p_max_size) {

fd = open(APMDEV, O_RDONLY);
if (fd < 0) {
snprintf(p, p_max_size, "ERR");
snprintf(p, p_max_size, "%s", "ERR");
return;
}

if (apm_getinfo(fd, &a_info) != 0) {
close(fd);
snprintf(p, p_max_size, "ERR");
snprintf(p, p_max_size, "%s", "ERR");
return;
}
close(fd);
Expand Down Expand Up @@ -165,13 +165,13 @@ void print_apm_battery_time(struct text_object *obj, char *p, int p_max_size) {

fd = open(APMDEV, O_RDONLY);
if (fd < 0) {
snprintf(p, p_max_size, "ERR");
snprintf(p, p_max_size, "%s", "ERR");
return;
}

if (apm_getinfo(fd, &a_info) != 0) {
close(fd);
snprintf(p, p_max_size, "ERR");
snprintf(p, p_max_size, "%s", "ERR");
return;
}
close(fd);
Expand All @@ -182,7 +182,7 @@ void print_apm_battery_time(struct text_object *obj, char *p, int p_max_size) {
batt_time = a_info.ai_batt_time;

if (batt_time == -1) {
snprintf(p, p_max_size, "unknown");
snprintf(p, p_max_size, "%s", "unknown");
} else
#ifdef __OpenBSD__
{
Expand Down
6 changes: 3 additions & 3 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ std::string variable_substitute(std::string s) {
std::string var;
std::string::size_type l = 0;

if (isalpha(s[pos + 1]) != 0) {
if (isalpha((unsigned char)s[pos + 1]) != 0) {
l = 1;
while (pos + l < s.size() && (isalnum(s[pos + l]) != 0)) {
while (pos + l < s.size() && (isalnum((unsigned char)s[pos + l]) != 0)) {
++l;
}
var = s.substr(pos + 1, l - 1);
Expand Down Expand Up @@ -323,7 +323,7 @@ unsigned int round_to_int(float f) {

void scan_loadavg_arg(struct text_object *obj, const char *arg) {
obj->data.i = 0;
if ((arg != nullptr) && (arg[1] == 0) && (isdigit(arg[0]) != 0)) {
if ((arg != nullptr) && (arg[1] == 0) && (isdigit((unsigned char)arg[0]) != 0)) {
obj->data.i = atoi(arg);
if (obj->data.i > 3 || obj->data.i < 1) {
NORM_ERR("loadavg arg needs to be in range (1,3)");
Expand Down
22 changes: 16 additions & 6 deletions src/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
obj->callbacks.free = &gen_free_opaque;
#endif /* !__OpenBSD__ */
END OBJ(freq, nullptr) get_cpu_count();
if ((arg == nullptr) || (isdigit(arg[0]) == 0) || strlen(arg) >= 3 ||
if ((arg == nullptr) || (isdigit((unsigned char)arg[0]) == 0) || strlen(arg) >= 3 ||
atoi(&arg[0]) == 0 || atoi(&arg[0]) > info.cpu_count) {
obj->data.i = 1;
/* NORM_ERR("freq: Invalid CPU number or you don't have that many CPUs! "
Expand All @@ -435,7 +435,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
}
obj->callbacks.print = &print_freq;
END OBJ(freq_g, nullptr) get_cpu_count();
if ((arg == nullptr) || (isdigit(arg[0]) == 0) || strlen(arg) >= 3 ||
if ((arg == nullptr) || (isdigit((unsigned char)arg[0]) == 0) || strlen(arg) >= 3 ||
atoi(&arg[0]) == 0 || atoi(&arg[0]) > info.cpu_count) {
obj->data.i = 1;
/* NORM_ERR("freq_g: Invalid CPU number or you don't have that many "
Expand All @@ -461,7 +461,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
obj->callbacks.free = &free_tcp_ping;
#if defined(__linux__)
END OBJ(voltage_mv, 0) get_cpu_count();
if (!arg || !isdigit(arg[0]) || strlen(arg) >= 3 || atoi(&arg[0]) == 0 ||
if (!arg || !isdigit((unsigned char)arg[0]) || strlen(arg) >= 3 || atoi(&arg[0]) == 0 ||
atoi(&arg[0]) > info.cpu_count) {
obj->data.i = 1;
/* NORM_ERR("voltage_mv: Invalid CPU number or you don't have that many "
Expand All @@ -471,7 +471,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
}
obj->callbacks.print = &print_voltage_mv;
END OBJ(voltage_v, 0) get_cpu_count();
if (!arg || !isdigit(arg[0]) || strlen(arg) >= 3 || atoi(&arg[0]) == 0 ||
if (!arg || !isdigit((unsigned char)arg[0]) || strlen(arg) >= 3 || atoi(&arg[0]) == 0 ||
atoi(&arg[0]) > info.cpu_count) {
obj->data.i = 1;
/* NORM_ERR("voltage_v: Invalid CPU number or you don't have that many "
Expand Down Expand Up @@ -815,6 +815,16 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
obj->data.s = strndup(arg ? arg : "", text_buffer_size.get(*state));
obj->callbacks.print = &print_cat;
obj->callbacks.free = &gen_free_opaque;

#ifdef BUILD_X11
END OBJ(num_led, 0)
obj->callbacks.print = &print_num_led;
END OBJ(caps_led, 0)
obj->callbacks.print = &print_caps_led;
END OBJ(scroll_led, 0)
obj->callbacks.print = &print_scroll_led;
#endif /* BUILD_X11 */

END OBJ(catp, 0)
obj->data.s = strndup(arg ? arg : "", text_buffer_size.get(*state));
obj->callbacks.print = &print_catp;
Expand Down Expand Up @@ -1997,7 +2007,7 @@ int extract_variable_text_internal(struct text_object *retval,
s = p;
if (*p == '#') { p++; }
while ((*p != 0) &&
((isalnum(static_cast<int>(*p)) != 0) || *p == '_')) {
((isalnum((unsigned char)(*p)) != 0) || *p == '_')) {
p++;
}
}
Expand Down Expand Up @@ -2031,7 +2041,7 @@ int extract_variable_text_internal(struct text_object *retval,
arg = strchr(buf, ' ');
*arg = '\0';
arg++;
while (isspace(static_cast<int>(*arg)) != 0) { arg++; }
while (isspace((unsigned char)(*arg)) != 0) { arg++; }
if (*arg == 0) { arg = nullptr; }
}

Expand Down
2 changes: 1 addition & 1 deletion src/fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void get_fs_type(const char *path, char *result) {
void init_fs_bar(struct text_object *obj, const char *arg) {
arg = scan_bar(obj, arg, 1);
if (arg != nullptr) {
while (isspace(*arg) != 0) { arg++; }
while (isspace((unsigned char)*arg) != 0) { arg++; }
if (*arg == '\0') { arg = "/"; }
} else {
arg = "/";
Expand Down
2 changes: 1 addition & 1 deletion src/hddtemp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void print_hddtemp(struct text_object *obj, char *p, int p_max_size) {
char unit;

if (get_hddtemp_info(obj->data.s, &val, &unit)) {
snprintf(p, p_max_size, "N/A");
snprintf(p, p_max_size, "%s", "N/A");
} else {
temp_print(p, p_max_size, (double)val,
(unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
Expand Down
6 changes: 3 additions & 3 deletions src/i8k.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ void print_i8k_ac_status(struct text_object *obj, char *p, int p_max_size) {

sscanf(i8k.ac_status, "%d", &ac_status);
if (ac_status == -1) {
snprintf(p, p_max_size, "disabled (read i8k docs)");
snprintf(p, p_max_size, "%s", "disabled (read i8k docs)");
}
if (ac_status == 0) {
snprintf(p, p_max_size, "off");
snprintf(p, p_max_size, "%s", "off");
}
if (ac_status == 1) {
snprintf(p, p_max_size, "on");
snprintf(p, p_max_size, "%s", "on");
}
}

Expand Down
48 changes: 24 additions & 24 deletions src/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void print_ioscheduler(struct text_object *obj, char *p, int p_max_size) {
}
fclose(fp);
out_fail:
snprintf(p, p_max_size, "n/a");
snprintf(p, p_max_size, "%s", "n/a");
return;
}

Expand Down Expand Up @@ -410,7 +410,7 @@ int update_net_stats(void) {
p = buf;
/* change char * p to first non-space character, which is the beginning
* of the interface name */
while (*p != '\0' && isspace((int)*p)) { p++; }
while (*p != '\0' && isspace((unsigned char)*p)) { p++; }

s = p;

Expand Down Expand Up @@ -573,7 +573,7 @@ int update_net_stats(void) {
if (winfo->b.essid_on) {
snprintf(ns->essid, 32, "%s", winfo->b.essid);
} else {
snprintf(ns->essid, 32, "off/any");
snprintf(ns->essid, 32, "%s", "off/any");
}
}
// get channel and freq
Expand Down Expand Up @@ -864,7 +864,7 @@ int update_stat(void) {
sscanf(buf, "%*s %hu", &info.run_threads);
} else if (strncmp(buf, "cpu", 3) == 0) {
double delta;
if (isdigit(buf[3])) {
if (isdigit((unsigned char)buf[3])) {
idx++; // just increment here since the CPU index can skip numbers
} else {
idx = 0;
Expand Down Expand Up @@ -1426,15 +1426,15 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) {

/* yeah, slow... :/ */
if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep)) {
snprintf(p_client_buffer, client_buffer_size, "no fans?");
snprintf(p_client_buffer, client_buffer_size, "%s", "no fans?");
return;
}

snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf);

fp = open_file(buf2, &rep);
if (!fp) {
snprintf(p_client_buffer, client_buffer_size,
snprintf(p_client_buffer, client_buffer_size, "%s",
"can't open fan's state file");
return;
}
Expand Down Expand Up @@ -1504,15 +1504,15 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size,
} else {
/* yeah, slow... :/ */
if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
snprintf(p_client_buffer, client_buffer_size, "%s", "no ac_adapters?");
return;
}

snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);

fp = open_file(buf2, &rep);
if (!fp) {
snprintf(p_client_buffer, client_buffer_size,
snprintf(p_client_buffer, client_buffer_size, "%s",
"No ac adapter found.... where is it?");
return;
}
Expand Down Expand Up @@ -1821,12 +1821,12 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat,
"charging %d%%",
(int)(((float)remaining_capacity / acpi_last_full[idx]) * 100));
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
} else {
strncpy(last_battery_str[idx], "charging",
sizeof(last_battery_str[idx]) - 1);
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
}
}
/* discharging */
Expand All @@ -1845,14 +1845,14 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat,
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx]) - 1,
"full");
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
} else {
snprintf(
last_battery_str[idx], sizeof(last_battery_str[idx]) - 1,
"discharging %d%%",
(int)(((float)remaining_capacity / acpi_last_full[idx]) * 100));
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
}
}
/* charged */
Expand Down Expand Up @@ -1951,12 +1951,12 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat,
"charging %d%%",
(int)((remaining_capacity * 100) / acpi_last_full[idx]));
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
} else {
strncpy(last_battery_str[idx], "charging",
sizeof(last_battery_str[idx]) - 1);
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
}
/* discharging */
} else if (strncmp(charging_state, "discharging", 64) == 0) {
Expand All @@ -1973,13 +1973,13 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat,
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx]) - 1,
"charged");
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
} else {
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx]) - 1,
"discharging %d%%",
(int)((remaining_capacity * 100) / acpi_last_full[idx]));
snprintf(last_battery_time_str[idx],
sizeof(last_battery_time_str[idx]) - 1, "unknown");
sizeof(last_battery_time_str[idx]) - 1, "%s", "unknown");
}
/* charged */
} else if (strncmp(charging_state, "charged", 64) == 0) {
Expand Down Expand Up @@ -2021,7 +2021,7 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat,

if (life == -1) {
/* could check now that there is ac */
snprintf(last_battery_str[idx], 64, "not present");
snprintf(last_battery_str[idx], 64, "%s", "not present");

/* could check that status == 3 here? */
} else if (ac && life != 100) {
Expand Down Expand Up @@ -2307,10 +2307,10 @@ void get_powerbook_batt_info(struct text_object *obj, char *buffer, int n) {
if (timeval == 0 && ac && (flags & PMU_BATT_PRESENT) &&
!(flags & PMU_BATT_CHARGING)) {
snprintf(pb_battery_info[PB_BATT_PERCENT],
sizeof(pb_battery_info[PB_BATT_PERCENT]), "100%%");
sizeof(pb_battery_info[PB_BATT_PERCENT]), "%s", "100%%");
} else if (timeval == 0) {
snprintf(pb_battery_info[PB_BATT_PERCENT],
sizeof(pb_battery_info[PB_BATT_PERCENT]), "unknown");
sizeof(pb_battery_info[PB_BATT_PERCENT]), "%s", "unknown");
} else {
snprintf(pb_battery_info[PB_BATT_PERCENT],
sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
Expand All @@ -2320,7 +2320,7 @@ void get_powerbook_batt_info(struct text_object *obj, char *buffer, int n) {
/* update time string */
if (timeval == 0) { /* fully charged or battery not present */
snprintf(pb_battery_info[PB_BATT_TIME],
sizeof(pb_battery_info[PB_BATT_TIME]), "unknown");
sizeof(pb_battery_info[PB_BATT_TIME]), "%s", "unknown");
} else if (timeval < 60 * 60) { /* don't show secs */
format_seconds_short(pb_battery_info[PB_BATT_TIME],
sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
Expand Down Expand Up @@ -2371,16 +2371,16 @@ void print_disk_protect_queue(struct text_object *obj, char *p,
snprintf(path, 127, "/sys/block/%s/queue/protect", obj->data.s);
}
if ((fp = fopen(path, "r")) == nullptr) {
snprintf(p, p_max_size, "n/a ");
snprintf(p, p_max_size, "%s", "n/a ");
return;
}
if (fscanf(fp, "%d\n", &state) != 1) {
fclose(fp);
snprintf(p, p_max_size, "failed");
snprintf(p, p_max_size, "%s", "failed");
return;
}
fclose(fp);
snprintf(p, p_max_size, (state > 0) ? "frozen" : "free ");
snprintf(p, p_max_size, "%s", (state > 0) ? "frozen" : "free ");
}

std::unordered_map<std::string, bool> dev_list;
Expand Down Expand Up @@ -2454,7 +2454,7 @@ void print_distribution(struct text_object *obj, char *p, int p_max_size) {
struct stat sb;

if (stat("/etc/arch-release", &sb) == 0) {
snprintf(p, p_max_size, "Arch Linux");
snprintf(p, p_max_size, "%s", "Arch Linux");
return;
}
snprintf(p, p_max_size, "Unknown");
Expand Down
Loading

0 comments on commit 7bca0ca

Please sign in to comment.