Browse Source

Using strncpy and calloc for safety

Piotr Czajkowski 5 years ago
parent
commit
71fba4091e
1 changed files with 6 additions and 4 deletions
  1. 6 4
      fileinfo.c

+ 6 - 4
fileinfo.c

@@ -7,11 +7,13 @@ struct fileInfo* newFileInfo(const char *filePath) {
 	if (!strrchr(filePath, '.')) return NULL;
 
 	struct fileInfo *info = malloc(sizeof(struct fileInfo));
-	info->fullname = malloc(strlen(filePath)+1);
-	strcpy(info->fullname, filePath);
+	size_t filePathLength = strlen(filePath);
+	info->fullname = calloc(filePathLength+1, 1);
+	strncpy(info->fullname, filePath, filePathLength);
 	
-	info->_base = malloc(strlen(basename(info->fullname))+1);
-	strcpy(info->_base, basename(info->fullname));
+	size_t basenameLength = strlen(basename(info->fullname));
+	info->_base = calloc(basenameLength+1, 1);
+	strncpy(info->_base, basename(info->fullname), basenameLength);
 
 	char *dot = ".";
 	info->name = strtok(info->_base, dot);