forked from GreengageDB/greengage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_wrapper.h
More file actions
82 lines (70 loc) · 2.19 KB
/
string_wrapper.h
File metadata and controls
82 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*-----------------------------------------------------------------------
*
* string_wrapper.h
* Wrappers around string.h functions
*
* Portions Copyright (c) 2009, Greenplum Inc.
* Portions Copyright (c) 2012-Present Pivotal Software, Inc.
*
* IDENTIFICATION
* src/include/utils/string_wrapper.h
*
*-----------------------------------------------------------------------
*/
#ifndef _UTILS___STRING_WRAPPER_H
#define _UTILS___STRING_WRAPPER_H
#include <string.h>
#include <errno.h>
#include <utils/elog.h>
#include "utils/guc.h"
#define NULL_TO_DUMMY_STR(s) ((s) == NULL ? "<<null>>" : (s))
#define SAFE_STR_LENGTH(s) ((s) == NULL ? 0 : strlen(s))
static inline
size_t gp_strxfrm(char *dst, const char *src, size_t n)
{
size_t result;
errno = 0;
result = strxfrm(dst, src, n);
if ( errno != 0 )
{
if ( errno == EINVAL || errno == EILSEQ)
{
ereport(ERROR,
(errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
errmsg("Unable to process string for comparison or sorting because it contains data that "
"is not valid for the collation specified by LC_COLLATE ('%s'). "
"The string has length %lu "
"and value (limited to 100 characters): '%.100s'",
GetConfigOption("lc_collate", false, false),
(unsigned long) SAFE_STR_LENGTH(src),
NULL_TO_DUMMY_STR(src))));
}
else
{
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Unable to process string for comparison or sorting. Error: %s. "
"String has length %lu and value (limited to 100 characters): '%.100s'",
strerror(errno),
(unsigned long) SAFE_STR_LENGTH(src),
NULL_TO_DUMMY_STR(src))));
}
}
if (result >= n)
{
/* strxfrm documentation says if result >= n, contents of the dst are
* indeterminate.
* In this case, let's try again and return valid data.
*/
size_t dst_len = result;
char * tmp_dst = (char *) palloc(dst_len + 1);
result = strxfrm(tmp_dst, src, dst_len + 1);
Assert(result <= dst_len);
/* return a n-character null terminated string */
memcpy(dst, tmp_dst, n-1);
dst[n-1] = '\0';
pfree(tmp_dst);
}
return result;
}
#endif /* _UTILS___STRING_WRAPPER_H */