Skip to content

Commit 4958331

Browse files
committed
improve string handling
1 parent 2966218 commit 4958331

6 files changed

Lines changed: 191 additions & 88174 deletions

File tree

src/console.c

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@
140140

141141

142142
}};
143-
Size_t __strlen__(const Byte_t *str_) {
143+
Size_t __strlen__(const Byte_t *str_, const Size_t maxLen_) {
144144

145145
Size_t len = 0x0u;
146146

147-
if(__PointerIsNotNull__(str_)) {
147+
if(__PointerIsNotNull__(str_) && (maxLen_ > 0x0u)) {
148148

149-
while(CHAR_NULL != str_[len]) {
149+
while((len < maxLen_) && (CHAR_NULL != str_[len])) {
150150

151151
len++;
152152
}
@@ -177,7 +177,7 @@
177177

178178
if(__PointerIsNotNull__(dest_) && __PointerIsNotNull__(src_) && (0x0u < destSize_)) {
179179

180-
while((CHAR_NULL != src_[i]) && (i < (destSize_ - 0x1u))) {
180+
while((i < (destSize_ - 0x1u)) && (CHAR_NULL != src_[i])) {
181181

182182
dest_[i] = src_[i];
183183

@@ -239,16 +239,16 @@
239239
}
240240

241241

242-
Base_t __strcmp__(const Byte_t *s1_, const Byte_t *s2_) {
242+
Base_t __strcmp__(const Byte_t *s1_, const Byte_t *s2_, const Size_t maxLen_) {
243243

244244
Size_t i = 0x0u;
245245

246-
if(__PointerIsNull__(s1_) || __PointerIsNull__(s2_)) {
246+
if(__PointerIsNull__(s1_) || __PointerIsNull__(s2_) || (0x0u == maxLen_)) {
247247

248248
return (0x0u);
249249
}
250250

251-
while((CHAR_NULL != s1_[i]) && (CHAR_NULL != s2_[i])) {
251+
while((i < maxLen_) && (CHAR_NULL != s1_[i]) && (CHAR_NULL != s2_[i])) {
252252

253253
if(s1_[i] != s2_[i]) {
254254

@@ -258,12 +258,12 @@
258258
i++;
259259
}
260260

261-
if(s1_[i] == s2_[i]) {
261+
if((i < maxLen_) && (s1_[i] == s2_[i])) {
262262

263263
return (0x0u);
264264
}
265265

266-
return ((s1_[i] < s2_[i]) ? -0x1 : 0x1);
266+
return ((i < maxLen_) ? ((s1_[i] < s2_[i]) ? -0x1 : 0x1) : 0x0u);
267267
}
268268

269269

@@ -311,11 +311,11 @@
311311

312312
if(__PointerIsNotNull__(dest_) && __PointerIsNotNull__(src_) && (0x0u < destSize_)) {
313313

314-
destLen = __strlen__(dest_);
314+
destLen = __strlen__(dest_, destSize_);
315315

316316
if(destLen < destSize_) {
317317

318-
while((CHAR_NULL != src_[i]) && ((destLen + i) < (destSize_ - 0x1u))) {
318+
while(((destLen + i) < (destSize_ - 0x1u)) && (CHAR_NULL != src_[i])) {
319319

320320
dest_[destLen + i] = src_[i];
321321

@@ -340,46 +340,44 @@
340340
}
341341

342342

343-
Byte_t * __strchr__(const Byte_t *str_, const Byte_t ch_) {
343+
Byte_t * __strchr__(const Byte_t *str_, const Byte_t ch_, const Size_t maxLen_) {
344344

345345
Size_t i = 0x0u;
346346

347-
if(__PointerIsNull__(str_)) {
347+
if(__PointerIsNull__(str_) || (0x0u == maxLen_)) {
348348

349349
return (null);
350350
}
351351

352-
while(CHAR_NULL != str_[i]) {
352+
for(i = 0x0u; i < maxLen_; i++) {
353353

354354
if(str_[i] == ch_) {
355355

356356
return ((Byte_t *) &str_[i]);
357357
}
358358

359-
i++;
360-
}
361-
362-
if(CHAR_NULL == ch_) {
359+
if(CHAR_NULL == str_[i]) {
363360

364-
return ((Byte_t *) &str_[i]);
361+
break;
362+
}
365363
}
366364

367365
return (null);
368366
}
369367

370368

371-
Byte_t * __strrchr__(const Byte_t *str_, const Byte_t ch_) {
369+
Byte_t * __strrchr__(const Byte_t *str_, const Byte_t ch_, const Size_t maxLen_) {
372370

373371
Size_t len = 0x0u;
374372

375373
Size_t i = 0x0u;
376374

377-
if(__PointerIsNull__(str_)) {
375+
if(__PointerIsNull__(str_) || (0x0u == maxLen_)) {
378376

379377
return (null);
380378
}
381379

382-
len = __strlen__(str_);
380+
len = __strlen__(str_, maxLen_);
383381

384382
for(i = len; i > 0x0u; i--) {
385383

@@ -389,7 +387,7 @@
389387
}
390388
}
391389

392-
if((CHAR_NULL == ch_) && (len > 0x0u)) {
390+
if((CHAR_NULL == ch_) && (len < maxLen_)) {
393391

394392
return ((Byte_t *) &str_[len]);
395393
}
@@ -424,9 +422,9 @@
424422

425423
if(__PointerIsNotNull__(dest_) && __PointerIsNotNull__(base_) && __PointerIsNotNull__(path_) && (0x0u != destSize_)) {
426424

427-
baseLen = __strlen__(base_);
425+
baseLen = __strlen__(base_, destSize_);
428426

429-
pathLen = __strlen__(path_);
427+
pathLen = __strlen__(path_, destSize_);
430428

431429
if((0x0u != baseLen) && (0x0u != pathLen)) {
432430

@@ -537,7 +535,7 @@
537535

538536
if(__PointerIsNotNull__(path_) && (0x0u != pathSize_)) {
539537

540-
len = __strlen__(path_);
538+
len = __strlen__(path_, pathSize_);
541539

542540
if((0x0u != len) && (len < CONFIG_FS_MAX_PATH_LENGTH)) {
543541

@@ -595,7 +593,7 @@
595593

596594
Size_t m = 0x0u;
597595

598-
segLen = __strlen__(segments[k]);
596+
segLen = __strlen__(segments[k], CONFIG_FS_MAX_PATH_LENGTH);
599597

600598
if(k > 0x0u) {
601599

@@ -667,7 +665,7 @@
667665

668666
if(__PointerIsNotNull__(dest_) && __PointerIsNotNull__(path_) && (0x0u != destSize_)) {
669667

670-
len = __strlen__(path_);
668+
len = __strlen__(path_, destSize_);
671669

672670
if(0x0u == len) {
673671

@@ -757,7 +755,7 @@
757755

758756
if(__PointerIsNotNull__(dest_) && __PointerIsNotNull__(path_) && (0x0u != destSize_)) {
759757

760-
len = __strlen__(path_);
758+
len = __strlen__(path_, destSize_);
761759

762760
if(0x0u == len) {
763761

@@ -1031,7 +1029,7 @@
10311029

10321030
if(__PointerIsNotNull__(str_)) {
10331031

1034-
len = __strlen__(str_);
1032+
len = __strlen__(str_, 0xFFFFu);
10351033

10361034
if(0x0u < len) {
10371035

@@ -1319,7 +1317,7 @@
13191317

13201318
for(i = 0x0u; __PointerIsNotNull__(commandTable[i].name) && !commandFound; i++) {
13211319

1322-
if(0 == __strcmp__(cmdName, commandTable[i].name)) {
1320+
if(0 == __strcmp__(cmdName, commandTable[i].name, CONFIG_CONSOLE_MAX_COMMAND_LENGTH)) {
13231321

13241322
if(__PointerIsNotNull__(commandTable[i].handler)) {
13251323

@@ -1811,7 +1809,7 @@
18111809

18121810
pathBuilt = true;
18131811

1814-
} else if(0 == __strcmp__(args_, (const Byte_t *) "..")) {
1812+
} else if(0 == __strcmp__(args_, (const Byte_t *) "..", CONFIG_CONSOLE_MAX_COMMAND_LENGTH)) {
18151813

18161814
if(OK(__path_dirname__(newPath, consoleState.currentWorkingDirectory, CONFIG_FS_MAX_PATH_LENGTH))) {
18171815

src/console.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@
189189
#if defined(POSIX_ARCH_OTHER)
190190
void __ConsoleStateClear__(void);
191191
#endif /* if defined(POSIX_ARCH_OTHER) */
192-
Size_t __strlen__(const Byte_t *str_);
192+
Size_t __strlen__(const Byte_t *str_, const Size_t maxLen_);
193193
Return_t __strcpy__(Byte_t *dest_, const Byte_t *src_, const Size_t destSize_);
194194
Return_t __strncpy__(Byte_t *dest_, const Byte_t *src_, const Size_t n_);
195-
Base_t __strcmp__(const Byte_t *s1_, const Byte_t *s2_);
195+
Base_t __strcmp__(const Byte_t *s1_, const Byte_t *s2_, const Size_t maxLen_);
196196
Base_t __strncmp__(const Byte_t *s1_, const Byte_t *s2_, const Size_t n_);
197197
Return_t __strcat__(Byte_t *dest_, const Byte_t *src_, const Size_t destSize_);
198-
Byte_t * __strchr__(const Byte_t *str_, const Byte_t ch_);
199-
Byte_t * __strrchr__(const Byte_t *str_, const Byte_t ch_);
198+
Byte_t * __strchr__(const Byte_t *str_, const Byte_t ch_, const Size_t maxLen_);
199+
Byte_t * __strrchr__(const Byte_t *str_, const Byte_t ch_, const Size_t maxLen_);
200200
Return_t __path_join__(Byte_t *dest_, const Byte_t *base_, const Byte_t *path_, const Size_t destSize_);
201201
Return_t __path_normalize__(Byte_t *path_, const Size_t pathSize_);
202202
Base_t __path_is_absolute__(const Byte_t *path_);

0 commit comments

Comments
 (0)