Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include <stdbool.h>
#include <sys/types.h>

Expand Down
2 changes: 1 addition & 1 deletion CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ void CRT_init(int delay, int colorScheme, bool allowUnicode) {
setlocale(LC_CTYPE, "");

#ifdef HAVE_LIBNCURSESW
if (allowUnicode && strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
if (allowUnicode && String_eq(nl_langinfo(CODESET), "UTF-8"))
CRT_utf8 = true;
else
CRT_utf8 = false;
Expand Down
2 changes: 2 additions & 0 deletions FunctionBar.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "FunctionBar.h"

#include <stdlib.h>
Expand Down
2 changes: 2 additions & 0 deletions MainPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include <stdbool.h>
#include <sys/types.h>

Expand Down
2 changes: 1 addition & 1 deletion Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Meter* Meter_new(struct ProcessList_* pl, int param, const MeterClass* type) {
this->param = param;
this->pl = pl;
this->curItems = type->maxItems;
this->values = xCalloc(type->maxItems, sizeof(double));
this->values = type->maxItems ? xCalloc(type->maxItems, sizeof(double)) : NULL;
this->total = type->total;
this->caption = xStrdup(type->caption);
if (Meter_initFn(this))
Expand Down
2 changes: 2 additions & 0 deletions Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include <stdbool.h>
#include <sys/time.h>

Expand Down
11 changes: 4 additions & 7 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ void Settings_delete(Settings* this) {

static void Settings_readMeters(Settings* this, char* line, int column) {
char* trim = String_trim(line);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
char** ids = String_split(trim, ' ', NULL);
free(trim);
this->columns[column].names = ids;
}

static void Settings_readMeterModes(Settings* this, char* line, int column) {
char* trim = String_trim(line);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
char** ids = String_split(trim, ' ', NULL);
free(trim);
int len = 0;
for (int i = 0; ids[i]; i++) {
Expand Down Expand Up @@ -94,8 +92,7 @@ static void Settings_defaultMeters(Settings* this, int initialCpuCount) {

static void readFields(ProcessField* fields, int* flags, const char* line) {
char* trim = String_trim(line);
int nIds;
char** ids = String_split(trim, ' ', &nIds);
char** ids = String_split(trim, ' ', NULL);
free(trim);
int i, j;
*flags = 0;
Expand Down Expand Up @@ -126,7 +123,7 @@ static bool Settings_read(Settings* this, const char* fileName, int initialCpuCo
if (!line) {
break;
}
int nOptions;
size_t nOptions;
char** option = String_split(line, '=', &nOptions);
free (line);
if (nOptions < 2) {
Expand Down
2 changes: 2 additions & 0 deletions UsersTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "UsersTable.h"

#include <pwd.h>
Expand Down
80 changes: 38 additions & 42 deletions XUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ void fail() {
}

void* xMalloc(size_t size) {
assert(size > 0);
void* data = malloc(size);
if (!data && size > 0) {
if (!data) {
fail();
}
return data;
}

void* xCalloc(size_t nmemb, size_t size) {
assert(nmemb > 0);
assert(size > 0);
void* data = calloc(nmemb, size);
if (!data && nmemb > 0 && size > 0) {
if (!data) {
fail();
}
return data;
}

void* xRealloc(void* ptr, size_t size) {
if (!size) {
free(ptr);
return NULL;
}
assert(size > 0);
void* data = realloc(ptr, size); // deepcode ignore MemoryLeakOnRealloc: this goes to fail()
if (!data) {
free(ptr);
Expand All @@ -54,11 +54,11 @@ void* xRealloc(void* ptr, size_t size) {
}

char* String_cat(const char* s1, const char* s2) {
int l1 = strlen(s1);
int l2 = strlen(s2);
const size_t l1 = strlen(s1);
const size_t l2 = strlen(s2);
char* out = xMalloc(l1 + l2 + 1);
memcpy(out, s1, l1);
memcpy(out+l1, s2, l2+1);
memcpy(out+l1, s2, l2);
out[l1 + l2] = '\0';
return out;
}
Expand All @@ -67,39 +67,24 @@ char* String_trim(const char* in) {
while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
in++;
}
int len = strlen(in);

size_t len = strlen(in);
while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
len--;
}
char* out = xMalloc(len+1);
strncpy(out, in, len);
out[len] = '\0';
return out;
}

inline int String_eq(const char* s1, const char* s2) {
if (s1 == NULL || s2 == NULL) {
if (s1 == NULL && s2 == NULL)
return 1;
else
return 0;
}
return (strcmp(s1, s2) == 0);
return xStrndup(in, len);
}

char** String_split(const char* s, char sep, int* n) {
*n = 0;
const int rate = 10;
char** String_split(const char* s, char sep, size_t* n) {
const unsigned int rate = 10;
char** out = xCalloc(rate, sizeof(char*));
int ctr = 0;
int blocks = rate;
char* where;
size_t ctr = 0;
unsigned int blocks = rate;
const char* where;
while ((where = strchr(s, sep)) != NULL) {
int size = where - s;
char* token = xMalloc(size + 1);
strncpy(token, s, size);
token[size] = '\0';
out[ctr] = token;
size_t size = (size_t)(where - s);
out[ctr] = xStrndup(s, size);
ctr++;
if (ctr == blocks) {
blocks += rate;
Expand All @@ -113,36 +98,39 @@ char** String_split(const char* s, char sep, int* n) {
}
out = xRealloc(out, sizeof(char*) * (ctr + 1));
out[ctr] = NULL;
*n = ctr;

if (n)
*n = ctr;

return out;
}

void String_freeArray(char** s) {
if (!s) {
return;
}
for (int i = 0; s[i] != NULL; i++) {
for (size_t i = 0; s[i] != NULL; i++) {
free(s[i]);
}
free(s);
}

char* String_getToken(const char* line, const unsigned short int numMatch) {
const unsigned short int len = strlen(line);
const size_t len = strlen(line);
char inWord = 0;
unsigned short int count = 0;
char match[50];

unsigned short int foundCount = 0;
size_t foundCount = 0;

for (unsigned short int i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
char lastState = inWord;
inWord = line[i] == ' ' ? 0:1;

if (lastState == 0 && inWord == 1)
count++;

if(inWord == 1){
if (inWord == 1){
if (count == numMatch && line[i] != ' ' && line[i] != '\0' && line[i] != '\n' && line[i] != (char)EOF) {
match[foundCount] = line[i];
foundCount++;
Expand All @@ -155,8 +143,8 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
}

char* String_readLine(FILE* fd) {
const int step = 1024;
int bufSize = step;
const unsigned int step = 1024;
unsigned int bufSize = step;
char* buffer = xMalloc(step + 1);
char* at = buffer;
for (;;) {
Expand Down Expand Up @@ -213,3 +201,11 @@ char* xStrdup(const char* str) {
}
return data;
}

char* xStrndup(const char* str, size_t len) {
char* data = strndup(str, len);
if (!data) {
fail();
}
return data;
}
23 changes: 16 additions & 7 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in the source distribution for its full text.

#include "config.h" // IWYU pragma: keep

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> // IWYU pragma: keep
#include <string.h> // IWYU pragma: keep
Expand All @@ -24,25 +25,31 @@ void* xCalloc(size_t nmemb, size_t size);

void* xRealloc(void* ptr, size_t size);

#define String_startsWith(s, match) (strncmp((s),(match),strlen(match)) == 0)
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)

/*
* String_startsWith gives better performance if strlen(match) can be computed
* at compile time (e.g. when they are immutable string literals). :)
*/
static inline bool String_startsWith(const char* s, const char* match) {
return strncmp(s, match, strlen(match)) == 0;
}

static inline bool String_contains_i(const char* s1, const char* s2) {
return strcasestr(s1, s2) != NULL;
}

static inline bool String_eq(const char* s1, const char* s2) {
return strcmp(s1, s2) == 0;
}

char* String_cat(const char* s1, const char* s2);

char* String_trim(const char* in);

int String_eq(const char* s1, const char* s2);

char** String_split(const char* s, char sep, int* n);
char** String_split(const char* s, char sep, size_t* n);

void String_freeArray(char** s);

char* String_getToken(const char* line, const unsigned short int numMatch);
char* String_getToken(const char* line, unsigned short int numMatch);

char* String_readLine(FILE* fd);

Expand All @@ -54,4 +61,6 @@ int xSnprintf(char *buf, int len, const char* fmt, ...);

char* xStrdup(const char* str) ATTR_NONNULL;

char* xStrndup(const char* str, size_t len) ATTR_NONNULL;

#endif
2 changes: 1 addition & 1 deletion htop.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
exit(0);
case 's':
assert(optarg); /* please clang analyzer, cause optarg can be NULL in the 'u' case */
if (strcmp(optarg, "help") == 0) {
if (String_eq(optarg, "help")) {
for (int j = 1; j < Platform_numberOfFields; j++) {
const char* name = Process_fields[j].name;
if (name) printf ("%s\n", name);
Expand Down
4 changes: 2 additions & 2 deletions linux/Battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
if (!dirEntry)
break;
char* entryName = dirEntry->d_name;
if (strncmp(entryName, "BAT", 3))
if (String_startsWith(entryName, "BAT"))
continue;
batteries[nBatteries] = xStrdup(entryName);
nBatteries++;
Expand Down Expand Up @@ -128,7 +128,7 @@ static ACPresence procAcpiCheck(void) {
char *isOnline = String_getToken(line, 2);
free(line);

if (strcmp(isOnline, "on-line") == 0) {
if (String_eq(isOnline, "on-line")) {
isOn = AC_PRESENT;
} else {
isOn = AC_ABSENT;
Expand Down
Loading