1
1
openmpi/test/util/opal_basename.c
Brian Barrett e9e4d2a4bc Handle asprintf errors with opal_asprintf wrapper
The Open MPI code base assumed that asprintf always behaved like
the FreeBSD variant, where ptr is set to NULL on error.  However,
the C standard (and Linux) only guarantee that the return code will
be -1 on error and leave ptr undefined.  Rather than fix all the
usage in the code, we use opal_asprintf() wrapper instead, which
guarantees the BSD-like behavior of ptr always being set to NULL.
In addition to being correct, this will fix many, many warnings
in the Open MPI code base.

Signed-off-by: Brian Barrett <bbarrett@amazon.com>
2018-10-08 16:43:53 -07:00

72 строки
1.7 KiB
C

/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "support.h"
#include "opal/util/basename.h"
#include "opal/util/printf.h"
static void test(const char* in, const char* out);
int main(int argc, char* argv[])
{
test_init("opal_basename()");
test("foo.txt", "foo.txt");
test("/foo/bar/baz", "baz");
test("/yow.c", "yow.c");
test("/", "/");
test("foo.txt/", "foo.txt");
test("/foo/bar/baz/", "baz");
test("/yow.c/", "yow.c");
test("//", "/");
/* All done */
return test_finalize();
}
void test(const char* in, const char* out)
{
char *msg;
char *ret = opal_basename(in);
if (0 == strcmp(ret, out)) {
test_success();
} else {
opal_asprintf(&msg, "Mismatch: input \"%s\", expected \"%s\", got \"%s\"\n",
in, out, ret);
test_failure(msg);
free(msg);
}
if (NULL != ret) {
free(ret);
}
}