Skip to content

Commit

Permalink
testsuite: use strlcpy()
Browse files Browse the repository at this point in the history
Problem: strncpy() could leave a string unterminated if not
zero-initialized.

Switch strlcpy() wherever strncpy() is used.

Use sizeof() where appropriate instead of pmix "MAX" #defines,
since that removes all ambiguity about whether the #define includes
space for the NULL or not (it does not, but I have to keep checking).
  • Loading branch information
garlick committed Nov 11, 2021
1 parent 343215a commit 3e55954
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions t/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ libtestutil_la_SOURCES = \

test_ldadd = \
$(builddir)/libtestutil.la \
$(top_builddir)/src/common/libutil/libutil.la \
$(FLUX_OPTPARSE_LIBS) \
$(FLUX_IDSET_LIBS) \
$(OMPI_LIBS) \
Expand Down
5 changes: 3 additions & 2 deletions t/src/abort.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <stdio.h>
#include <flux/optparse.h>

#include "src/common/libutil/strlcpy.h"

#include "log.h"

const char *opt_usage = "[OPTIONS]";
Expand Down Expand Up @@ -75,8 +77,7 @@ int main (int argc, char **argv)
*/
pmix_proc_t proc;
pmix_value_t *valp;
strncpy (proc.nspace, self.nspace, PMIX_MAX_NSLEN);
proc.nspace[PMIX_MAX_NSLEN] = '\0';
strlcpy (proc.nspace, self.nspace, sizeof (proc.nspace));
proc.rank = PMIX_RANK_WILDCARD;
if ((rc = PMIx_Get (&proc, PMIX_JOB_SIZE, NULL, 0, &valp)) != PMIX_SUCCESS)
log_msg_exit ("PMIx_Get %s: %s", PMIX_JOB_SIZE, PMIx_Error_string (rc));
Expand Down
9 changes: 5 additions & 4 deletions t/src/barrier.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <flux/optparse.h>
#include <flux/idset.h>

#include "src/common/libutil/strlcpy.h"

#include "log.h"
#include "monotime.h"

Expand Down Expand Up @@ -52,7 +54,7 @@ void set_info_bool (pmix_info_t *info,
bool value = true;
if (!strcmp (optarg, "false"))
value = false;
strncpy (info->key, name, PMIX_MAX_KEYLEN);
strlcpy (info->key, name, sizeof (info->key));
info->flags = flags;
info->value.type = PMIX_BOOL;
info->value.data.flag = value;
Expand All @@ -63,7 +65,7 @@ void set_info_int (pmix_info_t *info,
int flags,
int optarg)
{
strncpy (info->key, name, PMIX_MAX_KEYLEN);
strlcpy (info->key, name, sizeof (info->key));
info->flags = flags;
info->value.type = PMIX_INT;
info->value.data.flag = optarg;
Expand Down Expand Up @@ -193,8 +195,7 @@ int main (int argc, char **argv)
*/
pmix_proc_t proc;
pmix_value_t *valp;
strncpy (proc.nspace, self.nspace, PMIX_MAX_NSLEN);
proc.nspace[PMIX_MAX_NSLEN] = '\0';
strlcpy (proc.nspace, self.nspace, sizeof (proc.nspace));
proc.rank = PMIX_RANK_WILDCARD;
if ((rc = PMIx_Get (&proc, PMIX_JOB_SIZE, NULL, 0, &valp)) != PMIX_SUCCESS)
log_msg_exit ("PMIx_Get %s: %s", PMIX_JOB_SIZE, PMIx_Error_string (rc));
Expand Down
11 changes: 5 additions & 6 deletions t/src/bizcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <stdio.h>
#include <stdint.h>

#include "src/common/libutil/strlcpy.h"

#include "log.h"
#include "monotime.h"

Expand Down Expand Up @@ -48,8 +50,7 @@ int main (int argc, char **argv)

/* Get the size
*/
strncpy (proc.nspace, self.nspace, PMIX_MAX_NSLEN);
proc.nspace[PMIX_MAX_NSLEN] = '\0';
strlcpy (proc.nspace, self.nspace, sizeof (proc.nspace));
proc.rank = PMIX_RANK_WILDCARD;
if ((rc = PMIx_Get (&proc,
PMIX_JOB_SIZE,
Expand Down Expand Up @@ -130,8 +131,7 @@ int main (int argc, char **argv)

/* Fence
*/
strncpy (info.key, PMIX_COLLECT_DATA, PMIX_MAX_KEYLEN);
info.key[PMIX_MAX_KEYLEN] = '\0';
strlcpy (info.key, PMIX_COLLECT_DATA, sizeof (info.key));
info.value.type = PMIX_BOOL;
info.value.data.flag = true;

Expand All @@ -148,8 +148,7 @@ int main (int argc, char **argv)
pmix_proc_t proc;
pmix_value_t *valp;

strncpy (proc.nspace, self.nspace, PMIX_MAX_NSLEN);
proc.nspace[PMIX_MAX_NSLEN] = '\0';
strlcpy (proc.nspace, self.nspace, sizeof (proc.nspace));
errno = 0;
proc.rank = strtoul (argv[optindex], NULL, 10);
if (errno != 0)
Expand Down
9 changes: 6 additions & 3 deletions t/src/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <stdio.h>
#include <flux/optparse.h>

#include "src/common/libutil/strlcpy.h"

#include "log.h"

const char *opt_usage = "[OPTIONS]";
Expand Down Expand Up @@ -72,8 +74,7 @@ int main (int argc, char **argv)
*/
pmix_proc_t proc;
pmix_value_t *valp;
strncpy (proc.nspace, self.nspace, PMIX_MAX_NSLEN);
proc.nspace[PMIX_MAX_NSLEN] = '\0';
strlcpy (proc.nspace, self.nspace, sizeof (proc.nspace));
proc.rank = PMIX_RANK_WILDCARD;
if ((rc = PMIx_Get (&proc, PMIX_JOB_SIZE, NULL, 0, &valp)) != PMIX_SUCCESS)
log_msg_exit ("PMIx_Get %s: %s", PMIX_JOB_SIZE, PMIx_Error_string (rc));
Expand All @@ -85,7 +86,9 @@ int main (int argc, char **argv)
/* Add any info options
*/
if (event_message) {
strncpy (info[ninfo].key, PMIX_EVENT_TEXT_MESSAGE, PMIX_MAX_KEYLEN);
strlcpy (info[ninfo].key,
PMIX_EVENT_TEXT_MESSAGE,
sizeof (info[ninfo].key));
info[ninfo].value.type = PMIX_STRING;
info[ninfo].value.data.string = (char *)event_message;
info[ninfo].flags = 0;
Expand Down

0 comments on commit 3e55954

Please sign in to comment.