Mercurial > trustbridge
comparison common/strhelp.c @ 143:b026e6d2a161
Make flawfinder (a bit more) happy.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Mon, 24 Mar 2014 16:09:47 +0100 |
parents | 5fa4791d6d0e |
children | bf4bfd8843bd |
comparison
equal
deleted
inserted
replaced
136:5fa4791d6d0e | 143:b026e6d2a161 |
---|---|
2 #include <stdbool.h> | 2 #include <stdbool.h> |
3 #include <stdio.h> | 3 #include <stdio.h> |
4 #include <stdlib.h> | 4 #include <stdlib.h> |
5 #include <string.h> | 5 #include <string.h> |
6 #include <assert.h> | 6 #include <assert.h> |
7 | |
8 /* Remarks regarding the "Flawfinder: ignore" comments in this file: | |
9 * | |
10 * - strlen: | |
11 * | |
12 * It's true that strlen might crash if input is not null | |
13 * terminated. But by design there is not safe way to get the | |
14 * length of an string in C, and defining an additional length | |
15 * parameter for string parameter will only transfere the problem to | |
16 * the caller. | |
17 */ | |
7 | 18 |
8 static void | 19 static void |
9 out_of_core(void) | 20 out_of_core(void) |
10 { | 21 { |
11 fputs("\nfatal: out of memory\n", stderr); | 22 fputs("\nfatal: out of memory\n", stderr); |
105 } | 116 } |
106 | 117 |
107 bool | 118 bool |
108 str_equal (char *s1, char *s2) | 119 str_equal (char *s1, char *s2) |
109 { | 120 { |
110 size_t l1 = strlen(s1); | 121 size_t l1 = strlen(s1); /* Flawfinder: ignore */ |
111 size_t l2 = strlen(s2); | 122 size_t l2 = strlen(s2); /* Flawfinder: ignore */ |
112 if ((l1 == l2) && | 123 if ((l1 == l2) && |
113 (strcmp(s1, s2) == 0)) | 124 (strcmp(s1, s2) == 0)) |
114 return true; | 125 return true; |
115 else | 126 else |
116 return false; | 127 return false; |
117 } | 128 } |
118 | 129 |
119 bool | 130 bool |
120 str_starts_with (char *s1, char *s2) | 131 str_starts_with (char *s1, char *s2) |
121 { | 132 { |
122 if (strncmp(s1, s2, strlen(s2)) == 0) | 133 size_t l2 = strlen(s2); /* Flawfinder: ignore */ |
134 if (strncmp(s1, s2, l2) == 0) | |
123 return true; | 135 return true; |
124 else | 136 else |
125 return false; | 137 return false; |
126 } | 138 } |
127 | 139 |
131 size_t i; | 143 size_t i; |
132 if (*s != NULL) | 144 if (*s != NULL) |
133 { | 145 { |
134 while (isspace(**s)) | 146 while (isspace(**s)) |
135 (*s)++; | 147 (*s)++; |
136 i = strlen(*s); | 148 i = strlen(*s); /* Flawfinder: ignore */ |
137 while (isspace((*s)[--i])) | 149 while (isspace((*s)[--i])) |
138 (*s)[i] = '\0'; | 150 (*s)[i] = '\0'; |
139 } | 151 } |
140 } | 152 } |