|
| 1 | +#include <errno.h> |
1 | 2 | #include <stdio.h> |
2 | 3 | #include <string.h> |
3 | 4 | #include <fcntl.h> |
@@ -138,52 +139,86 @@ char **str_split(char *a_str, const char a_delim) |
138 | 139 | } |
139 | 140 |
|
140 | 141 | /** |
141 | | - * @brief method returns true if it can extract needed info from path, otherwise false. |
142 | | - * In case of true, it also updates mountString, mountPoint and newCWD parameters |
| 142 | + * @brief method returns 0 if it can extract needed info from path, otherwise a negative error code. |
| 143 | + * In case of success, it also updates mountString, mountPoint and newCWD parameters |
143 | 144 | * It splits path by ":", and requires a minimum of 3 elements |
144 | 145 | * Example: if path = hdd0:__common:pfs:/retroarch/ then |
145 | 146 | * mountString = "pfs:" |
146 | 147 | * mountPoint = "hdd0:__common" |
147 | 148 | * newCWD = pfs:/retroarch/ |
148 | | - * return true |
| 149 | + * return 0 on success, negative error code on failure |
149 | 150 | */ |
150 | | -int getMountInfo(char *path, char *mountString, char *mountPoint, char *newCWD) |
| 151 | +int getMountInfo(char *path, char *mountString, size_t mountStringSize, char *mountPoint, size_t mountPointSize, char *newCWD, size_t newCWDSize) |
151 | 152 | { |
152 | | - int expected_items = 4; |
153 | 153 | int i = 0; |
154 | | - char *items[expected_items]; |
155 | 154 | char **tokens = str_split(path, ':'); |
| 155 | + int ret = -EINVAL; |
156 | 156 |
|
157 | 157 | if (!tokens) |
158 | | - return 0; |
| 158 | + return -ENOMEM; |
159 | 159 |
|
160 | | - for (i = 0; *(tokens + i); i++) { |
161 | | - if (i < expected_items) { |
162 | | - items[i] = *(tokens + i); |
163 | | - } else { |
164 | | - free(*(tokens + i)); |
165 | | - } |
166 | | - } |
| 160 | + for (i = 0; *(tokens + i); i++) |
| 161 | + ; |
167 | 162 |
|
168 | 163 | if (i < 3) |
169 | | - return 0; |
| 164 | + goto cleanup; |
170 | 165 |
|
171 | | - if (mountPoint != NULL) |
172 | | - sprintf(mountPoint, "%s:%s", items[0], items[1]); |
| 166 | + if (mountPoint != NULL) { |
| 167 | + if (mountPointSize == 0) { |
| 168 | + ret = -ENAMETOOLONG; |
| 169 | + goto cleanup; |
| 170 | + } |
| 171 | + int written = snprintf(mountPoint, mountPointSize, "%s:%s", tokens[0], tokens[1]); |
| 172 | + if (written < 0) { |
| 173 | + ret = -EIO; |
| 174 | + goto cleanup; |
| 175 | + } |
| 176 | + if ((size_t)written >= mountPointSize) { |
| 177 | + ret = -ENAMETOOLONG; |
| 178 | + goto cleanup; |
| 179 | + } |
| 180 | + } |
173 | 181 |
|
174 | | - if (mountString != NULL) |
175 | | - sprintf(mountString, "%s:", items[2]); |
| 182 | + if (mountString != NULL) { |
| 183 | + if (mountStringSize == 0) { |
| 184 | + ret = -ENAMETOOLONG; |
| 185 | + goto cleanup; |
| 186 | + } |
| 187 | + int written = snprintf(mountString, mountStringSize, "%s:", tokens[2]); |
| 188 | + if (written < 0) { |
| 189 | + ret = -EIO; |
| 190 | + goto cleanup; |
| 191 | + } |
| 192 | + if ((size_t)written >= mountStringSize) { |
| 193 | + ret = -ENAMETOOLONG; |
| 194 | + goto cleanup; |
| 195 | + } |
| 196 | + } |
176 | 197 |
|
177 | | - if (newCWD != NULL) |
178 | | - sprintf(newCWD, "%s:%s", items[2], i > 3 ? items[3] : ""); |
| 198 | + if (newCWD != NULL) { |
| 199 | + if (newCWDSize == 0) { |
| 200 | + ret = -ENAMETOOLONG; |
| 201 | + goto cleanup; |
| 202 | + } |
| 203 | + int written = snprintf(newCWD, newCWDSize, "%s:%s", tokens[2], i > 3 ? tokens[3] : ""); |
| 204 | + if (written < 0) { |
| 205 | + ret = -EIO; |
| 206 | + goto cleanup; |
| 207 | + } |
| 208 | + if ((size_t)written >= newCWDSize) { |
| 209 | + ret = -ENAMETOOLONG; |
| 210 | + goto cleanup; |
| 211 | + } |
| 212 | + } |
179 | 213 |
|
180 | | - free(items[0]); |
181 | | - free(items[1]); |
182 | | - free(items[2]); |
| 214 | + ret = 0; |
183 | 215 |
|
184 | | - if (i > 3) |
185 | | - free(items[3]); |
| 216 | +cleanup: |
| 217 | + for (int idx = 0; *(tokens + idx); idx++) { |
| 218 | + free(*(tokens + idx)); |
| 219 | + } |
| 220 | + free(tokens); |
186 | 221 |
|
187 | | - return 1; |
| 222 | + return ret; |
188 | 223 | } |
189 | 224 | #endif |
0 commit comments