source: tags/ms_r18q1/CORE/arb_string.h

Last change on this file was 16990, checked in by westram, 6 years ago
  • pointer overflow reported by 8.1-sanitizer was in fact pointer arithmetic applied to NULp
File size: 1.9 KB
Line 
1#ifndef ARB_STRING_H
2#define ARB_STRING_H
3
4#ifndef ARB_MEM_H
5#include "arb_mem.h"
6#endif
7#ifndef ARB_ASSERT_H
8#include "arb_assert.h"
9#endif
10#ifndef _GLIBCXX_CSTRING
11#include <cstring>
12#endif
13
14inline char *ARB_strdup(const char *str) {
15    char *dup = strdup(str);
16    if (!dup) arb_mem::failed_to_allocate(strlen(str));
17    return dup;
18}
19
20inline char *ARB_strduplen(const char *p, unsigned len) {
21    // fast replacement for strdup, if len is known
22    if (p) {
23        char *neu;
24
25        arb_assert(strlen(p) == len);
26        // Note: Common reason for failure: a zero-char was manually printed by a GBS_global_string...-function
27
28        ARB_alloc(neu, len+1);
29        memcpy(neu, p, len+1);
30        return neu;
31    }
32    return NULp;
33}
34
35inline char *ARB_strpartdup(const char *start, const char *end) {
36    /* strdup of a part of a string (including 'start' and 'end')
37     * 'end' may point behind end of string -> copy only till zero byte
38     * if 'start' is NULp   -> return NULp
39     * if 'end'   is NULp   -> copy whole string
40     * if 'end'=('start'-1) -> return ""
41     * if 'end'<('start'-1) -> return NULp
42     */
43
44    char *result;
45    if (start && end) {
46        int len = end-start+1;
47
48        if (len >= 0) {
49            const char *eos = (const char *)memchr(start, 0, len);
50
51            if (eos) len = eos-start;
52            ARB_alloc(result, len+1);
53            memcpy(result, start, len);
54            result[len] = 0;
55        }
56        else {
57            result = NULp;
58        }
59    }
60    else { // end = 0 -> return copy of complete string
61        result = nulldup(start);
62    }
63
64    return result;
65}
66
67inline char *ARB_strndup(const char *start, int len) {
68    return start ? ARB_strpartdup(start, start+len-1) : NULp;
69}
70
71
72const char *ARB_date_string(void);
73const char *ARB_dateTime_suffix(void);
74const char *ARB_keep_string(char *str);
75
76#else
77#error arb_string.h included twice
78#endif /* ARB_STRING_H */
Note: See TracBrowser for help on using the repository browser.