1
1
openmpi/opal/util/string_copy.c
Jeff Squyres 293a938d29 string_copy: assert() fail if the copy is too long
Add a hueristic: if the string copy is "too long", fail an assert().
This is based on the premise that Open MPI doesn't do large string
copies.  So if we see a dest_len that is over a certain threshhold
(currently set at 128K), this is likely a programmer error, and on
debug builds, we should fail an assert().  In production builds, it
will work just fine (assuming that it's not a programmer error).

Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
2018-10-01 13:34:15 -07:00

41 строка
1.0 KiB
C

/*
* Copyright (c) 2018 Cisco Systems, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <assert.h>
#include "opal/util/string_copy.h"
void opal_string_copy(char *dest, const char *src, size_t dest_len)
{
size_t i;
char *new_dest = dest;
// Open MPI does not do *giant* string copies. Hence, we use the
// hueristic: if "dest_len" is too large, this is a programmer
// error. We pseudo-arbitrarily pick a large value to be the max
// allowable dest_len: 128K. If we ever need to increase this
// value someday (because something has a legit reason to
// opal_string_copy() more than 128K), the core dumps that are
// generated by the assert() failure should make this fairly
// obvious.
assert(dest_len <= OPAL_MAX_SIZE_ALLOWED_BY_OPAL_STRING_COPY);
for (i = 0; i < dest_len; ++i, ++src, ++new_dest) {
*new_dest = *src;
if ('\0' == *src) {
return;
}
}
dest[i - 1] = '\0';
}