Merge pull request #290 from duskCoder/changes

Fix potential issues & avoid unnecessary instructions
This commit is contained in:
Ingo Bürk 2018-06-22 13:55:09 +02:00 committed by GitHub
commit 4d3344ab9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 15 additions and 14 deletions

View File

@ -106,7 +106,7 @@ static bool path_exists(const char *path) {
static void *scalloc(size_t size) { static void *scalloc(size_t size) {
void *result = calloc(size, 1); void *result = calloc(size, 1);
exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size); exit_if_null(result, "Error: out of memory (calloc(%zu))\n", size);
return result; return result;
} }
@ -142,7 +142,7 @@ static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value,
long num = strtol(value, &end, 10); long num = strtol(value, &end, 10);
if (num < 0) if (num < 0)
die("Invalid min_width attribute found in section %s, line %d: %d\n" die("Invalid min_width attribute found in section %s, line %d: %ld\n"
"Expected positive integer or string\n", "Expected positive integer or string\n",
context->name, context->line, num); context->name, context->line, num);
else if (num == LONG_MIN || num == LONG_MAX || (end && *end != '\0')) else if (num == LONG_MIN || num == LONG_MAX || (end && *end != '\0'))

View File

@ -186,7 +186,8 @@ char *sstrdup(const char *str);
/* src/general.c */ /* src/general.c */
char *skip_character(char *input, char character, int amount); char *skip_character(char *input, char character, int amount);
void die(const char *fmt, ...);
void die(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn));
bool slurp(const char *filename, char *destination, int size); bool slurp(const char *filename, char *destination, int size);
/* src/output.c */ /* src/output.c */

View File

@ -51,12 +51,10 @@ char *skip_character(char *input, char character, int amount) {
* *
*/ */
void die(const char *fmt, ...) { void die(const char *fmt, ...) {
char buffer[512];
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
(void)vsnprintf(buffer, sizeof(buffer), fmt, ap); (void)vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "%s", buffer);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View File

@ -450,14 +450,17 @@ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_ge
.present_rate = 0, .present_rate = 0,
.status = CS_UNKNOWN, .status = CS_UNKNOWN,
}; };
if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down)) if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down)) {
globfree(&globbuf);
free(globpath);
return false; return false;
}
is_found = true; is_found = true;
add_battery_info(batt_info, &batt_buf); add_battery_info(batt_info, &batt_buf);
} }
globfree(&globbuf);
} }
globfree(&globbuf);
free(globpath); free(globpath);
if (!is_found) { if (!is_found) {

View File

@ -223,7 +223,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
asprintf(&thermal_zone, THERMAL_ZONE, zone); asprintf(&thermal_zone, THERMAL_ZONE, zone);
else { else {
static glob_t globbuf; static glob_t globbuf;
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) != 0)
die("glob() failed\n"); die("glob() failed\n");
if (globbuf.gl_pathc == 0) { if (globbuf.gl_pathc == 0) {
/* No glob matches, the specified path does not contain a wildcard. */ /* No glob matches, the specified path does not contain a wildcard. */

View File

@ -7,7 +7,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include <sys/types.h> #include <sys/types.h>
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
#include <sys/param.h> #include <sys/param.h>
#include <sys/mount.h> #include <sys/mount.h>
#elif defined(__NetBSD__) #elif defined(__NetBSD__)

View File

@ -39,10 +39,9 @@ static int print_bytes_human(char *outwalk, uint64_t bytes) {
* *
*/ */
static long memory_absolute(const long mem_total, const char *size) { static long memory_absolute(const long mem_total, const char *size) {
long mem_absolute = -1;
char *endptr = NULL; char *endptr = NULL;
mem_absolute = strtol(size, &endptr, 10); long mem_absolute = strtol(size, &endptr, 10);
if (endptr) { if (endptr) {
while (endptr[0] != '\0' && isspace(endptr[0])) while (endptr[0] != '\0' && isspace(endptr[0]))

View File

@ -151,7 +151,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
snd_mixer_selem_id_set_index(sid, mixer_idx); snd_mixer_selem_id_set_index(sid, mixer_idx);
snd_mixer_selem_id_set_name(sid, mixer); snd_mixer_selem_id_set_name(sid, mixer);
if (!(elem = snd_mixer_find_selem(m, sid))) { if (!(elem = snd_mixer_find_selem(m, sid))) {
fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n", fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %u)\n",
snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
snd_mixer_close(m); snd_mixer_close(m);
snd_mixer_selem_id_free(sid); snd_mixer_selem_id_free(sid);

View File

@ -24,7 +24,7 @@ bool process_runs(const char *path) {
static glob_t globbuf; static glob_t globbuf;
memset(pidbuf, 0, sizeof(pidbuf)); memset(pidbuf, 0, sizeof(pidbuf));
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) != 0)
die("glob() failed\n"); die("glob() failed\n");
if (globbuf.gl_pathc == 0) { if (globbuf.gl_pathc == 0) {
/* No glob matches, the specified path does not contain a wildcard. */ /* No glob matches, the specified path does not contain a wildcard. */