Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix PMPI_Error_string.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 14 May 2019 13:23:18 +0000 (15:23 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 14 May 2019 19:19:59 +0000 (21:19 +0200)
MPI_MAX_ERROR_STRING was wrongly used for higher error code.
Use snprintf and ensure the string is properly null terminated.

src/smpi/bindings/smpi_pmpi.cpp

index a20a5ff..933894a 100644 (file)
@@ -214,17 +214,16 @@ int PMPI_Error_class(int errorcode, int* errorclass) {
   return MPI_SUCCESS;
 }
 
   return MPI_SUCCESS;
 }
 
-int PMPI_Error_string(int errorcode, char* string, int* resultlen){
-  if (errorcode<0 || errorcode>= MPI_MAX_ERROR_STRING || string ==nullptr){
+int PMPI_Error_string(int errorcode, char* string, int* resultlen)
+{
+  static const char* smpi_error_string[] = {FOREACH_ERROR(GENERATE_STRING)};
+  constexpr int nerrors                  = (sizeof smpi_error_string) / (sizeof smpi_error_string[0]);
+  if (errorcode < 0 || errorcode >= nerrors || string == nullptr)
     return MPI_ERR_ARG;
     return MPI_ERR_ARG;
-  } else {
-    static const char *smpi_error_string[] = {
-      FOREACH_ERROR(GENERATE_STRING)
-    };
-    *resultlen = strlen(smpi_error_string[errorcode]);
-    memcpy(string, smpi_error_string[errorcode], *resultlen*sizeof(char));
-    return MPI_SUCCESS;  
-  }
+
+  int len    = snprintf(string, MPI_MAX_ERROR_STRING, "%s", smpi_error_string[errorcode]);
+  *resultlen = std::min(len, MPI_MAX_ERROR_STRING - 1);
+  return MPI_SUCCESS;
 }
 
 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
 }
 
 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {