Skip to content
Snippets Groups Projects
Commit f9687bbc authored by Tim Wickberg's avatar Tim Wickberg
Browse files

Replace strncpy with strlcpy and simplify string terminations.

parent 2acb052c
No related branches found
No related tags found
No related merge requests found
...@@ -97,6 +97,7 @@ ...@@ -97,6 +97,7 @@
#include "src/api/slurm_pmi.h" #include "src/api/slurm_pmi.h"
#include "src/common/macros.h" #include "src/common/macros.h"
#include "src/common/strlcpy.h"
#include "src/slurmd/slurmstepd/slurmstepd_job.h" #include "src/slurmd/slurmstepd/slurmstepd_job.h"
#define KVS_STATE_LOCAL 0 #define KVS_STATE_LOCAL 0
...@@ -899,7 +900,7 @@ static void _init_kvs( char kvsname[] ) ...@@ -899,7 +900,7 @@ static void _init_kvs( char kvsname[] )
kvs_recs[i].kvs_name = malloc(PMI_MAX_KVSNAME_LEN); kvs_recs[i].kvs_name = malloc(PMI_MAX_KVSNAME_LEN);
if (!kvs_recs[i].kvs_name) if (!kvs_recs[i].kvs_name)
pmi_nomem_error(__FILE__, __LINE__, "_init_kvs"); pmi_nomem_error(__FILE__, __LINE__, "_init_kvs");
strncpy(kvs_recs[i].kvs_name, kvsname, PMI_MAX_KVSNAME_LEN); strlcpy(kvs_recs[i].kvs_name, kvsname, PMI_MAX_KVSNAME_LEN);
kvs_recs[i].kvs_state = KVS_STATE_LOCAL; kvs_recs[i].kvs_state = KVS_STATE_LOCAL;
kvs_recs[i].kvs_cnt = 0; kvs_recs[i].kvs_cnt = 0;
kvs_recs[i].kvs_inx = 0; kvs_recs[i].kvs_inx = 0;
...@@ -1154,7 +1155,7 @@ static int _kvs_put( const char kvsname[], const char key[], const char value[], ...@@ -1154,7 +1155,7 @@ static int _kvs_put( const char kvsname[], const char key[], const char value[],
rc = PMI_FAIL; rc = PMI_FAIL;
} else { } else {
rc = PMI_SUCCESS; rc = PMI_SUCCESS;
strncpy(kvs_recs[i].kvs_values[j], value, strlcpy(kvs_recs[i].kvs_values[j], value,
PMI_MAX_VAL_LEN); PMI_MAX_VAL_LEN);
} }
goto fini; goto fini;
...@@ -1188,9 +1189,9 @@ no_dup: ...@@ -1188,9 +1189,9 @@ no_dup:
rc = PMI_FAIL; rc = PMI_FAIL;
} else { } else {
rc = PMI_SUCCESS; rc = PMI_SUCCESS;
strncpy(kvs_recs[i].kvs_values[j], value, strlcpy(kvs_recs[i].kvs_values[j], value,
PMI_MAX_VAL_LEN); PMI_MAX_VAL_LEN);
strncpy(kvs_recs[i].kvs_keys[j], key, PMI_MAX_KEY_LEN); strlcpy(kvs_recs[i].kvs_keys[j], key, PMI_MAX_KEY_LEN);
} }
goto fini; goto fini;
} }
...@@ -1378,7 +1379,7 @@ int PMI_KVS_Get( const char kvsname[], const char key[], char value[], int lengt ...@@ -1378,7 +1379,7 @@ int PMI_KVS_Get( const char kvsname[], const char key[], char value[], int lengt
if (strlen(kvs_recs[i].kvs_values[j]) > (length-1)) if (strlen(kvs_recs[i].kvs_values[j]) > (length-1))
rc = PMI_ERR_INVALID_LENGTH; rc = PMI_ERR_INVALID_LENGTH;
else { else {
strncpy(value, kvs_recs[i].kvs_values[j], strlcpy(value, kvs_recs[i].kvs_values[j],
length); length);
rc = PMI_SUCCESS; rc = PMI_SUCCESS;
} }
...@@ -1461,8 +1462,8 @@ int PMI_KVS_Iter_first(const char kvsname[], char key[], int key_len, char val[] ...@@ -1461,8 +1462,8 @@ int PMI_KVS_Iter_first(const char kvsname[], char key[], int key_len, char val[]
if (kvs_recs[i].kvs_key_states[j] == KVS_KEY_STATE_DISABLED) if (kvs_recs[i].kvs_key_states[j] == KVS_KEY_STATE_DISABLED)
continue; continue;
strncpy(key, kvs_recs[i].kvs_keys[j], key_len); strlcpy(key, kvs_recs[i].kvs_keys[j], key_len);
strncpy(val, kvs_recs[i].kvs_values[j], val_len); strlcpy(val, kvs_recs[i].kvs_values[j], val_len);
kvs_recs[i].kvs_inx = j; kvs_recs[i].kvs_inx = j;
rc = PMI_SUCCESS; rc = PMI_SUCCESS;
goto fini; goto fini;
...@@ -1539,8 +1540,8 @@ int PMI_KVS_Iter_next(const char kvsname[], char key[], int key_len, ...@@ -1539,8 +1540,8 @@ int PMI_KVS_Iter_next(const char kvsname[], char key[], int key_len,
if (kvs_recs[i].kvs_key_states[j] == KVS_KEY_STATE_DISABLED) if (kvs_recs[i].kvs_key_states[j] == KVS_KEY_STATE_DISABLED)
continue; continue;
strncpy(key, kvs_recs[i].kvs_keys[j], key_len); strlcpy(key, kvs_recs[i].kvs_keys[j], key_len);
strncpy(val, kvs_recs[i].kvs_values[j], val_len); strlcpy(val, kvs_recs[i].kvs_values[j], val_len);
kvs_recs[i].kvs_inx = j; kvs_recs[i].kvs_inx = j;
rc = PMI_SUCCESS; rc = PMI_SUCCESS;
goto fini; goto fini;
...@@ -1690,8 +1691,7 @@ int PMI_Parse_option(int num_args, char *args[], int *num_parsed, ...@@ -1690,8 +1691,7 @@ int PMI_Parse_option(int num_args, char *args[], int *num_parsed,
PMI_Free_keyvals(temp, s); PMI_Free_keyvals(temp, s);
return PMI_FAIL; return PMI_FAIL;
} }
strncpy(temp[s].key, kp, len); strlcpy(temp[s].key, kp, len);
temp[s].key[len] = '\0';
if (!IsPmiKey(temp[s].key)) { if (!IsPmiKey(temp[s].key)) {
free(temp[s].key); free(temp[s].key);
temp[s].key = NULL; temp[s].key = NULL;
...@@ -1707,8 +1707,7 @@ int PMI_Parse_option(int num_args, char *args[], int *num_parsed, ...@@ -1707,8 +1707,7 @@ int PMI_Parse_option(int num_args, char *args[], int *num_parsed,
PMI_Free_keyvals(temp, s+1); PMI_Free_keyvals(temp, s+1);
return PMI_FAIL; return PMI_FAIL;
} }
strncpy(temp[s].val, vp, len); strlcpy(temp[s].val, vp, len);
temp[s].val[len] = '\0';
s++; s++;
i++; // try next args i++; // try next args
cp = args[i]; cp = args[i];
...@@ -1922,8 +1921,7 @@ int PMI_Get_options(char *str, int *length) ...@@ -1922,8 +1921,7 @@ int PMI_Get_options(char *str, int *length)
optlen = strlen(pmi_opt_str); optlen = strlen(pmi_opt_str);
if (*length <= optlen) { if (*length <= optlen) {
strncpy(str, pmi_opt_str, *length-1); strlcpy(str, pmi_opt_str, *length);
str[*length-1] = '\0';
return PMI_ERR_NOMEM; return PMI_ERR_NOMEM;
} }
...@@ -1937,8 +1935,7 @@ static int IsPmiKey(char * key) { ...@@ -1937,8 +1935,7 @@ static int IsPmiKey(char * key) {
if (pmi_debug) if (pmi_debug)
fprintf(stderr, "In: IsPmiKey\n"); fprintf(stderr, "In: IsPmiKey\n");
strncpy(strh, key, 4); strlcpy(strh, key, 5);
strh[4]='\0';
if (!strcmp(strh, "PMI_") && (strlen(key) > 4)) { if (!strcmp(strh, "PMI_") && (strlen(key) > 4)) {
return 1; return 1;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment