Explorar o código

src: improve string processing

Nathalie Furmento %!s(int64=8) %!d(string=hai) anos
pai
achega
812db0016c

+ 1 - 1
src/common/fxt.c

@@ -94,7 +94,7 @@ static void _starpu_profile_set_tracefile(void)
 	char suffix[128];
 	snprintf(suffix, sizeof(suffix), "prof_file_%s_%d", user, _starpu_id);
 
-	strcat(_STARPU_PROF_FILE_USER, suffix);
+	strncat(_STARPU_PROF_FILE_USER, suffix, sizeof(_STARPU_PROF_FILE_USER));
 }
 
 void starpu_profiling_set_id(int new_id)

+ 1 - 4
src/common/utils.c

@@ -194,11 +194,8 @@ char *_starpu_mktemp(const char *directory, int flags, int *fd)
 	const char *tmp = "STARPU_XXXXXX";
 	char *baseCpy;
 	_STARPU_MALLOC(baseCpy, strlen(directory)+1+strlen(tmp)+1);
-	STARPU_ASSERT(baseCpy != NULL);
 
-	strcpy(baseCpy, directory);
-	strcat(baseCpy,"/");
-	strcat(baseCpy,tmp);
+	snprintf(baseCpy, strlen(directory)+1+strlen(tmp)+1, "%s/%s", directory, tmp);
 
 #if defined(STARPU_HAVE_WINDOWS)
 	_mktemp(baseCpy);

+ 6 - 9
src/core/disk_ops/disk_hdf5.c

@@ -445,9 +445,8 @@ static void *starpu_hdf5_plug(void *parameter, starpu_ssize_t size STARPU_ATTRIB
         else
         {
                 /* Well, open it ! */
-                char * path;
-                _STARPU_MALLOC(path, strlen((char *) parameter)+1);
-                strcpy(path, (char *) parameter);
+                char *path = strdup((char *)parameter);
+		STARPU_ASSERT(path);
 
                 fileBase->fileID = H5Fopen((char *)parameter, H5F_ACC_RDWR, H5P_DEFAULT);
                 if (fileBase->fileID < 0) 
@@ -545,9 +544,7 @@ static void *starpu_hdf5_alloc(void *base, size_t size)
 
         /* name in HDF5 is like a path */
         _STARPU_MALLOC(name, 1+strlen(prefix)+strlen(name_id)+1);
-        strcpy(name, "/");
-        strcat(name, prefix);
-        strcat(name, name_id);
+	snprintf(name, 1+strlen(prefix)+strlen(name_id)+1, "/%s%s", prefix, name_id);
 
         obj = _starpu_hdf5_data_alloc(fileBase, name, size);
 
@@ -585,10 +582,10 @@ static void *starpu_hdf5_open(void *base, void *pos, size_t size)
 {
         struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) base;
         struct starpu_hdf5_obj * obj;
-        char * name;
+        char *name;
 
-        _STARPU_MALLOC(name, strlen(pos)+1);
-        strcpy(name, (char *) pos);
+	name = strdup((char *)pos);
+	STARPU_ASSERT(name);
 
         obj = _starpu_hdf5_data_open(fileBase, name, size);
 

+ 9 - 12
src/core/disk_ops/disk_stdio.c

@@ -183,9 +183,8 @@ static void *starpu_stdio_open(void *base, void *pos, size_t size)
 	/* create template */
 	char *baseCpy;
 	_STARPU_MALLOC(baseCpy, strlen(fileBase->path)+1+strlen(pos)+1);
-	strcpy(baseCpy,(char *) fileBase->path);
-	strcat(baseCpy,(char *) "/");
-	strcat(baseCpy,(char *) pos);
+
+	snprintf(baseCpy, strlen(fileBase->path)+1+strlen(pos)+1, "%s/%s", fileBase->path, (char *)pos);
 
 	int id = open(baseCpy, O_RDWR);
 	if (id < 0)
@@ -327,19 +326,17 @@ static int starpu_stdio_full_write(void *base STARPU_ATTRIBUTE_UNUSED, void *obj
 static void *starpu_stdio_plug(void *parameter, starpu_ssize_t size STARPU_ATTRIBUTE_UNUSED)
 {
 	struct starpu_stdio_base * base;
+	struct stat buf;
+
 	_STARPU_MALLOC(base, sizeof(*base));
 	base->created = 0;
+	base->path = strdup((char *) parameter);
+	STARPU_ASSERT(base->path);
 
-	_STARPU_MALLOC(base->path, sizeof(char)*(strlen(parameter)+1));
-	strcpy(base->path,(char *) parameter);
-
+	if (!(stat(base->path, &buf) == 0 && S_ISDIR(buf.st_mode)))
 	{
-		struct stat buf;
-		if (!(stat(base->path, &buf) == 0 && S_ISDIR(buf.st_mode)))
-		{
-			_starpu_mkpath(base->path, S_IRWXU);
-			base->created = 1;
-		}
+		_starpu_mkpath(base->path, S_IRWXU);
+		base->created = 1;
 	}
 
 	return (void *) base;

+ 10 - 13
src/core/disk_ops/unistd/disk_unistd_global.c

@@ -188,13 +188,12 @@ void starpu_unistd_global_free(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, si
 /* open an existing memory on disk */
 void *starpu_unistd_global_open(struct starpu_unistd_global_obj *obj, void *base, void *pos, size_t size)
 {
-	struct starpu_unistd_base * fileBase = (struct starpu_unistd_base *) base;
+	struct starpu_unistd_base *fileBase = (struct starpu_unistd_base *) base;
 	/* create template */
 	char *baseCpy;
 	_STARPU_MALLOC(baseCpy, strlen(fileBase->path)+1+strlen(pos)+1);
-	strcpy(baseCpy,(char *) fileBase->path);
-	strcat(baseCpy,(char *) "/");
-	strcat(baseCpy,(char *) pos);
+
+	snprintf(baseCpy, strlen(fileBase->path)+1+strlen(pos)+1, "%s/%s", fileBase->path, (char *)pos);
 
 	int id = open(baseCpy, obj->flags);
 	if (id < 0)
@@ -471,19 +470,17 @@ int starpu_unistd_global_full_write(void *base STARPU_ATTRIBUTE_UNUSED, void *ob
 void *starpu_unistd_global_plug(void *parameter, starpu_ssize_t size STARPU_ATTRIBUTE_UNUSED)
 {
 	struct starpu_unistd_base * base;
+	struct stat buf;
+
 	_STARPU_MALLOC(base, sizeof(*base));
 	base->created = 0;
+	base->path = strdup((char *) parameter);
+	STARPU_ASSERT(base->path);
 
-	_STARPU_MALLOC(base->path, sizeof(char)*(strlen(parameter)+1));
-	strcpy(base->path,(char *) parameter);
-
+	if (!(stat(base->path, &buf) == 0 && S_ISDIR(buf.st_mode)))
 	{
-		struct stat buf;
-		if (!(stat(base->path, &buf) == 0 && S_ISDIR(buf.st_mode)))
-		{
-			_starpu_mkpath(base->path, S_IRWXU);
-			base->created = 1;
-		}
+		_starpu_mkpath(base->path, S_IRWXU);
+		base->created = 1;
 	}
 
 #if defined(HAVE_LIBAIO_H)

+ 1 - 0
src/core/topology.c

@@ -584,6 +584,7 @@ _starpu_init_mic_node (struct _starpu_machine_config *config, int mic_idx,
 	/* Let's get the helper program to run on the MIC device */
 	int mic_file_found =
 	    _starpu_src_common_locate_file (mic_sink_program_path,
+					    sizeof(mic_sink_program_path),
 					    starpu_getenv("STARPU_MIC_SINK_PROGRAM_NAME"),
 					    starpu_getenv("STARPU_MIC_SINK_PROGRAM_PATH"),
 					    user_conf->mic_sink_program_path,

+ 2 - 2
src/debug/traces/starpu_fxt.c

@@ -1298,8 +1298,8 @@ static void create_paje_state_if_not_found(char *name, struct starpu_fxt_options
 
 	/* it's the first time ... */
 	struct _starpu_symbol_name *entry = _starpu_symbol_name_new();
-	_STARPU_MALLOC(entry->name, strlen(name) + 1);
-	strcpy(entry->name, name);
+	entry->name = strdup(name);
+	STARPU_ASSERT(entry->name);
 
 	_starpu_symbol_name_list_push_front(&symbol_list, entry);
 

+ 23 - 25
src/drivers/mp_common/source_common.c

@@ -766,37 +766,35 @@ int _starpu_src_common_copy_sink_to_sink_async(struct _starpu_mp_node *src_node,
 /* 5 functions to determine the executable to run on the device (MIC, SCC,
  * MPI).
  */
-static void _starpu_src_common_cat_3(char *final, const char *first,
-		const char *second, const char *third)
+static void _starpu_src_common_cat_3(char *final, const size_t len, const char *first,
+				     const char *second, const char *third)
 {
-	strcpy(final, first);
-	strcat(final, second);
-	strcat(final, third);
+	snprintf(final, len, "%s%s%s", first, second, third);
 }
 
-static void _starpu_src_common_cat_2(char *final, const char *first, const char *second)
+static void _starpu_src_common_cat_2(char *final, const size_t len, const char *first, const char *second)
 {
-	_starpu_src_common_cat_3(final, first, second, "");
+	_starpu_src_common_cat_3(final, len, first, second, "");
 }
 
-static void _starpu_src_common_dir_cat(char *final, const char *dir, const char *file)
+static void _starpu_src_common_dir_cat(char *final, const size_t len, const char *dir, const char *file)
 {
 	if (file[0] == '/')
 		++file;
 
 	size_t size = strlen(dir);
 	if (dir[size - 1] == '/')
-		_starpu_src_common_cat_2(final, dir, file);
+		_starpu_src_common_cat_2(final, len, dir, file);
 	else
-		_starpu_src_common_cat_3(final, dir, "/", file);
+		_starpu_src_common_cat_3(final, len, dir, "/", file);
 }
 
-static int _starpu_src_common_test_suffixes(char *located_file_name, const char *base, const char **suffixes)
+static int _starpu_src_common_test_suffixes(char *located_file_name, const size_t len, const char *base, const char **suffixes)
 {
 	unsigned int i;
 	for (i = 0; suffixes[i] != NULL; ++i)
 	{
-		_starpu_src_common_cat_2(located_file_name, base, suffixes[i]);
+		_starpu_src_common_cat_2(located_file_name, len, base, suffixes[i]);
 		if (access(located_file_name, R_OK) == 0)
 			return 0;
 	}
@@ -804,21 +802,21 @@ static int _starpu_src_common_test_suffixes(char *located_file_name, const char
 	return 1;
 }
 
-int _starpu_src_common_locate_file(char *located_file_name,
-		const char *env_file_name, const char *env_mic_path,
-		const char *config_file_name, const char *actual_file_name,
-		const char **suffixes)
+int _starpu_src_common_locate_file(char *located_file_name, size_t len,
+				   const char *env_file_name, const char *env_mic_path,
+				   const char *config_file_name, const char *actual_file_name,
+				   const char **suffixes)
 {
 	if (env_file_name != NULL)
 	{
 		if (access(env_file_name, R_OK) == 0)
 		{
-			strcpy(located_file_name, env_file_name);
+			strncpy(located_file_name, env_file_name, len);
 			return 0;
 		}
 		else if(env_mic_path != NULL)
 		{
-			_starpu_src_common_dir_cat(located_file_name, env_mic_path, env_file_name);
+			_starpu_src_common_dir_cat(located_file_name, len, env_mic_path, env_file_name);
 
 			return access(located_file_name, R_OK);
 		}
@@ -827,40 +825,40 @@ int _starpu_src_common_locate_file(char *located_file_name,
 	{
 		if (access(config_file_name, R_OK) == 0)
 		{
-			strcpy(located_file_name, config_file_name);
+			strncpy(located_file_name, config_file_name, len);
 			return 0;
 		}
 		else if (env_mic_path != NULL)
 		{
-			_starpu_src_common_dir_cat(located_file_name, env_mic_path, config_file_name);
+			_starpu_src_common_dir_cat(located_file_name, len, env_mic_path, config_file_name);
 
 			return access(located_file_name, R_OK);
 		}
 	}
 	else if (actual_file_name != NULL)
 	{
-		if (_starpu_src_common_test_suffixes(located_file_name, actual_file_name, suffixes) == 0)
+		if (_starpu_src_common_test_suffixes(located_file_name, len, actual_file_name, suffixes) == 0)
 			return 0;
 
 		if (env_mic_path != NULL)
 		{
 			char actual_cpy[1024];
-			strcpy(actual_cpy, actual_file_name);
+			strncpy(actual_cpy, actual_file_name, sizeof(actual_cpy));
 
 			char *last =  strrchr(actual_cpy, '/');
 			while (last != NULL)
 			{
 				char tmp[1024];
 
-				_starpu_src_common_dir_cat(tmp, env_mic_path, last);
+				_starpu_src_common_dir_cat(tmp, sizeof(tmp), env_mic_path, last);
 
 				if (access(tmp, R_OK) == 0)
 				{
-					strcpy(located_file_name, tmp);
+					strncpy(located_file_name, tmp, len);
 					return 0;
 				}
 
-				if (_starpu_src_common_test_suffixes(located_file_name, tmp, suffixes) == 0)
+				if (_starpu_src_common_test_suffixes(located_file_name, len, tmp, suffixes) == 0)
 					return 0;
 
 				*last = '\0';

+ 1 - 1
src/drivers/mp_common/source_common.h

@@ -72,7 +72,7 @@ int _starpu_src_common_copy_sink_to_host_async(struct _starpu_mp_node *mp_node,
 int _starpu_src_common_copy_sink_to_sink_async(struct _starpu_mp_node *src_node,
 					 struct _starpu_mp_node *dst_node, void *src, void *dst, size_t size, void *event);
 
-int _starpu_src_common_locate_file(char *located_file_name,
+int _starpu_src_common_locate_file(char *located_file_name, size_t len,
 				   const char *env_file_name, const char *env_mic_path,
 				   const char *config_file_name, const char *actual_file_name,
 				   const char **suffixes);