Bladeren bron

avoid using sprintf

Samuel Thibault 7 jaren geleden
bovenliggende
commit
d12dc07efa

+ 1 - 1
src/drivers/opencl/driver_opencl.c

@@ -93,7 +93,7 @@ static void _starpu_opencl_limit_gpu_mem_if_needed(unsigned devid)
 	if (limit == -1)
 	{
 		char name[30];
-		sprintf(name, "STARPU_LIMIT_OPENCL_%u_MEM", devid);
+		snprintf(name, sizeof(name), "STARPU_LIMIT_OPENCL_%u_MEM", devid);
 		limit = starpu_get_env_number(name);
 	}
 #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)

+ 3 - 3
src/drivers/opencl/driver_opencl_utils.c

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  * Copyright (C) 2010, 2011, 2012, 2014, 2015, 2016, 2017  CNRS
- * Copyright (C) 2010-2016  Université de Bordeaux
+ * Copyright (C) 2010-2017  Université de Bordeaux
  *
  * StarPU is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -429,11 +429,11 @@ int _starpu_opencl_compile_or_load_opencl_from_file(const char *source_file_name
 
 	if (!strcmp(located_dir_name, ""))
 	{
-		sprintf(new_build_options, "%s", build_options);
+		snprintf(new_build_options, sizeof(new_build_options), "%s", build_options);
 	}
 	else
 	{
-		sprintf(new_build_options, "-I %s %s", located_dir_name, build_options);
+		snprintf(new_build_options, sizeof(new_build_options), "-I %s %s", located_dir_name, build_options);
 	}
 	_STARPU_DEBUG("Build options: <%s>\n", new_build_options);
 

+ 3 - 2
tests/errorcheck/workers_cpuid.c

@@ -77,13 +77,14 @@ static void copy_cpuid_array(long *dst, long *src, unsigned n)
 static char *array_to_str(long *array, int n)
 {
 	int i;
-	char *str = malloc(n * 3 * sizeof(long));
+	int len = n * 3 * sizeof(long);
+	char *str = malloc(len);
 	char *ptr = str;
 
 	for (i=0; i<n; i++)
 	{
 		int nchar;
-		nchar = sprintf(ptr, "%ld ", array[i]);
+		nchar = snprintf(ptr, len - (ptr-str), "%ld ", array[i]);
 		ptr += nchar;
 	}
 

+ 4 - 6
tests/loader.c

@@ -1,6 +1,6 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
- * Copyright (C) 2010, 2014-2016  Université de Bordeaux
+ * Copyright (C) 2010, 2014-2017  Université de Bordeaux
  *
  * StarPU is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -235,17 +235,15 @@ int main(int argc, char *argv[])
 	if (strstr(test_name, "spmv/dw_block_spmv"))
 	{
 		test_args = (char *) calloc(150, sizeof(char));
-		sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
+		snprintf(test_args, 150, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
 	}
 	else if (strstr(test_name, "starpu_perfmodel_display"))
 	{
-		test_args = (char *) calloc(5, sizeof(char));
-		sprintf(test_args, "-l");
+		test_args = strdup("-l");
 	}
 	else if (strstr(test_name, "starpu_perfmodel_plot"))
 	{
-		test_args = (char *) calloc(5, sizeof(char));
-		sprintf(test_args, "-l");
+		test_args = strdup("-l");
 	}
 
 	/* get launcher program */