From b845f0b9783f90699da022a1174965689828d153 Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Wed, 6 Sep 2017 07:01:45 -0700 Subject: [PATCH] opal_path_nfs: ensure arrays are always long enough This test used to have fixed-sized arrays for the mounts that it was checking. However, we periodically run across machines with more mounts than can fit into those fixed-size arrays. Rather than periodically increasing the size of those arrays (after re-discovering that the error is due to fixed-size arrays), just count how many entries there are and make arrays that are big enough. Additionally, add a check to ensure that we don't go over the max size of the array when reading/filling them. Signed-off-by: Jeff Squyres (cherry picked from commit dee8cfbfd0b75dd2443ade13f5955498eb6e0c5d) --- test/util/opal_path_nfs.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/test/util/opal_path_nfs.c b/test/util/opal_path_nfs.c index e2405bdefe4..b5fad7ae3dd 100644 --- a/test/util/opal_path_nfs.c +++ b/test/util/opal_path_nfs.c @@ -12,7 +12,7 @@ * All rights reserved. * Copyright (c) 2010 Oak Ridge National Laboratory. * All rights reserved. - * Copyright (c) 2010-2014 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2010-2017 Cisco Systems, Inc. All rights reserved * Copyright (c) 2010 IBM Corporation. All rights reserved. * Copyright (c) 2014 Los Alamos National Security, LLC. All rights * reserved. @@ -132,7 +132,6 @@ void test(char* file, bool expect) void get_mounts (int * num_dirs, char ** dirs[], bool * nfs[]) { -#define MAX_DIR 256 #define SIZE 1024 char * cmd = "mount | cut -f3,5 -d' ' > opal_path_nfs.out"; int rc; @@ -150,13 +149,31 @@ void get_mounts (int * num_dirs, char ** dirs[], bool * nfs[]) **dirs = NULL; *nfs = NULL; } - dirs_tmp = (char**) calloc (MAX_DIR, sizeof(char**)); - nfs_tmp = (bool*) malloc (MAX_DIR * sizeof(bool)); + /* First, count how many mount points there are. Previous + versions of this test tried to have a (large) constant-sized + array for the mount points, but periodically it would break + because we would run this test on a system with a larger number + of mount points than the array. So just count and make sure to + have an array large enough. */ file = fopen("opal_path_nfs.out", "r"); + int count = 0; + while (NULL != fgets (buffer, SIZE, file)) { + ++count; + } + printf("Found %d mounts\n", count); + + // Add one more so we can have a NULL entry at the end + ++count; + + dirs_tmp = (char**) calloc (count, sizeof(char*)); + nfs_tmp = (bool*) calloc (count, sizeof(bool)); + i = 0; rc = 4711; - while (NULL != fgets (buffer, SIZE, file)) { + rewind(file); + // i should never be more than count, but be safe anyway. + while (i < count && NULL != fgets (buffer, SIZE, file)) { int mount_known; char fs[MAXNAMLEN];