From 041274604961bc5633c70f9c3db90f46117c508e Mon Sep 17 00:00:00 2001 From: gramanas Date: Tue, 23 Oct 2018 23:04:03 +0300 Subject: Secure ckutil functions --- src/ckutil.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/ckutil.c b/src/ckutil.c index 272f737..6ce736c 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -20,6 +20,9 @@ #include "ckutil.h" int util_is_dir(const char *path) { + if (!path) { + return 0; + } DIR *dir; dir = opendir(path); if (!dir) { @@ -30,6 +33,9 @@ int util_is_dir(const char *path) { } void util_replace_slash_with_uscore(char *s) { + if (!s) { + return; + } int i = 0; while (*s != '\0') { if (*s == '/' && i != 0) { @@ -41,6 +47,9 @@ void util_replace_slash_with_uscore(char *s) { } int util_file_exists(const char* path, char *absPath) { + if (!path || !absPath) { + return 0; + } struct stat st = {0}; if (stat(path, &st) == -1) { return 0; @@ -52,6 +61,9 @@ int util_file_exists(const char* path, char *absPath) { } int util_is_file_rw(const char *path) { + if (!path) { + return 0; + } if (access(path, R_OK | W_OK) == 0) { return 1; } @@ -59,6 +71,9 @@ int util_is_file_rw(const char *path) { } int util_is_file_link(const char *path) { + if (!path) { + return 0; + } struct stat buf; lstat(path, &buf); if (S_ISLNK(buf.st_mode)) { @@ -68,10 +83,16 @@ int util_is_file_link(const char *path) { } void util_mkdir(const char *name) { + if (!name) { + return; + } mkdir(name, 0755); } int util_move_file(const char *path, const char* dest) { + if (!path || !dest) { + return 0; + } int srcFile = open(path, O_RDONLY); int destFile = open(dest, O_WRONLY | O_CREAT); struct stat st, newSt; @@ -93,11 +114,17 @@ int util_move_file(const char *path, const char* dest) { } int util_symlink_file(const char *path, const char* dest) { + if (!path || !dest) { + return -1; + } return symlink(path, dest); } void str_make_ck_config_name(char *ret, const char *path, const char *progName) { + if (!path || !ret || !progName) { + return; + } char *basec = strdup(path); char *bname = basename(basec); @@ -107,6 +134,9 @@ void str_make_ck_config_name(char *ret, const char *path, void str_join_dirname_with_basename(char *ret, const char *dirname, const char *basename) { + if (!dirname || !ret || !basename) { + return; + } strcpy(ret, dirname); strcat(ret, "/"); strcat(ret, basename); -- cgit v1.2.3