1 //===-- sanitizer_flags.cc ------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_flags.h"
14 #include "sanitizer_common.h"
15 #include "sanitizer_libc.h"
17 namespace __sanitizer
{
19 static bool GetFlagValue(const char *env
, const char *name
,
20 const char **value
, int *value_length
) {
23 const char *pos
= internal_strstr(env
, name
);
27 pos
+= internal_strlen(name
);
34 end
= internal_strchr(pos
, '"');
35 } else if (pos
[0] == '\'') {
37 end
= internal_strchr(pos
, '\'');
39 end
= internal_strchr(pos
, ' ');
42 end
= pos
+ internal_strlen(pos
);
45 *value_length
= end
- pos
;
49 static bool StartsWith(const char *flag
, int flag_length
, const char *value
) {
52 int value_length
= internal_strlen(value
);
53 return (flag_length
>= value_length
) &&
54 (0 == internal_strncmp(flag
, value
, value_length
));
57 void ParseFlag(const char *env
, bool *flag
, const char *name
) {
60 if (!GetFlagValue(env
, name
, &value
, &value_length
))
62 if (StartsWith(value
, value_length
, "0") ||
63 StartsWith(value
, value_length
, "no") ||
64 StartsWith(value
, value_length
, "false"))
66 if (StartsWith(value
, value_length
, "1") ||
67 StartsWith(value
, value_length
, "yes") ||
68 StartsWith(value
, value_length
, "true"))
72 void ParseFlag(const char *env
, int *flag
, const char *name
) {
75 if (!GetFlagValue(env
, name
, &value
, &value_length
))
77 *flag
= internal_atoll(value
);
80 static LowLevelAllocator allocator_for_flags
;
82 void ParseFlag(const char *env
, const char **flag
, const char *name
) {
85 if (!GetFlagValue(env
, name
, &value
, &value_length
))
87 // Copy the flag value. Don't use locks here, as flags are parsed at
89 char *value_copy
= (char*)(allocator_for_flags
.Allocate(value_length
+ 1));
90 internal_memcpy(value_copy
, value
, value_length
);
91 value_copy
[value_length
] = '\0';
95 } // namespace __sanitizer