Skip to content

Commit 2ab150c

Browse files
committed
Removed toolchain specific warnings
- Comparisons with differently signed integer types - Incorrectly signed constant - Unreachable default returns - Leaked uninitialized variables in relocate goto statements
1 parent 0825d34 commit 2ab150c

File tree

1 file changed

+135
-131
lines changed

1 file changed

+135
-131
lines changed

lfs.c

+135-131
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_dir_t *dir) {
373373
// set defaults
374374
dir->d.rev += 1;
375375
dir->d.size = sizeof(dir->d)+4;
376-
dir->d.tail[0] = -1;
377-
dir->d.tail[1] = -1;
376+
dir->d.tail[0] = 0xffffffff;
377+
dir->d.tail[1] = 0xffffffff;
378378
dir->off = sizeof(dir->d);
379379

380380
// don't write out yet, let caller take care of that
@@ -455,88 +455,91 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
455455
bool relocated = false;
456456

457457
while (true) {
458-
int err = lfs_bd_erase(lfs, dir->pair[0]);
459-
if (err) {
460-
if (err == LFS_ERR_CORRUPT) {
461-
goto relocate;
458+
if (true) {
459+
int err = lfs_bd_erase(lfs, dir->pair[0]);
460+
if (err) {
461+
if (err == LFS_ERR_CORRUPT) {
462+
goto relocate;
463+
}
464+
return err;
462465
}
463-
return err;
464-
}
465466

466-
uint32_t crc = 0xffffffff;
467-
lfs_crc(&crc, &dir->d, sizeof(dir->d));
468-
err = lfs_bd_prog(lfs, dir->pair[0], 0, &dir->d, sizeof(dir->d));
469-
if (err) {
470-
if (err == LFS_ERR_CORRUPT) {
471-
goto relocate;
467+
uint32_t crc = 0xffffffff;
468+
lfs_crc(&crc, &dir->d, sizeof(dir->d));
469+
err = lfs_bd_prog(lfs, dir->pair[0], 0, &dir->d, sizeof(dir->d));
470+
if (err) {
471+
if (err == LFS_ERR_CORRUPT) {
472+
goto relocate;
473+
}
474+
return err;
472475
}
473-
return err;
474-
}
475476

476-
int i = 0;
477-
lfs_off_t oldoff = sizeof(dir->d);
478-
lfs_off_t newoff = sizeof(dir->d);
479-
while (newoff < (0x7fffffff & dir->d.size)-4) {
480-
if (i < count && regions[i].oldoff == oldoff) {
481-
lfs_crc(&crc, regions[i].newdata, regions[i].newlen);
482-
int err = lfs_bd_prog(lfs, dir->pair[0],
483-
newoff, regions[i].newdata, regions[i].newlen);
484-
if (err) {
485-
if (err == LFS_ERR_CORRUPT) {
486-
goto relocate;
477+
int i = 0;
478+
lfs_off_t oldoff = sizeof(dir->d);
479+
lfs_off_t newoff = sizeof(dir->d);
480+
while (newoff < (0x7fffffff & dir->d.size)-4) {
481+
if (i < count && regions[i].oldoff == oldoff) {
482+
lfs_crc(&crc, regions[i].newdata, regions[i].newlen);
483+
int err = lfs_bd_prog(lfs, dir->pair[0],
484+
newoff, regions[i].newdata, regions[i].newlen);
485+
if (err) {
486+
if (err == LFS_ERR_CORRUPT) {
487+
goto relocate;
488+
}
489+
return err;
487490
}
488-
return err;
489-
}
490491

491-
oldoff += regions[i].oldlen;
492-
newoff += regions[i].newlen;
493-
i += 1;
494-
} else {
495-
uint8_t data;
496-
int err = lfs_bd_read(lfs, oldpair[1], oldoff, &data, 1);
497-
if (err) {
498-
return err;
499-
}
492+
oldoff += regions[i].oldlen;
493+
newoff += regions[i].newlen;
494+
i += 1;
495+
} else {
496+
uint8_t data;
497+
int err = lfs_bd_read(lfs, oldpair[1], oldoff, &data, 1);
498+
if (err) {
499+
return err;
500+
}
500501

501-
lfs_crc(&crc, &data, 1);
502-
err = lfs_bd_prog(lfs, dir->pair[0], newoff, &data, 1);
503-
if (err) {
504-
if (err == LFS_ERR_CORRUPT) {
505-
goto relocate;
502+
lfs_crc(&crc, &data, 1);
503+
err = lfs_bd_prog(lfs, dir->pair[0], newoff, &data, 1);
504+
if (err) {
505+
if (err == LFS_ERR_CORRUPT) {
506+
goto relocate;
507+
}
508+
return err;
506509
}
507-
return err;
508-
}
509510

510-
oldoff += 1;
511-
newoff += 1;
511+
oldoff += 1;
512+
newoff += 1;
513+
}
512514
}
513-
}
514515

515-
err = lfs_bd_prog(lfs, dir->pair[0], newoff, &crc, 4);
516-
if (err) {
517-
if (err == LFS_ERR_CORRUPT) {
518-
goto relocate;
516+
err = lfs_bd_prog(lfs, dir->pair[0], newoff, &crc, 4);
517+
if (err) {
518+
if (err == LFS_ERR_CORRUPT) {
519+
goto relocate;
520+
}
521+
return err;
519522
}
520-
return err;
521-
}
522523

523-
err = lfs_bd_sync(lfs);
524-
if (err) {
525-
if (err == LFS_ERR_CORRUPT) {
526-
goto relocate;
524+
err = lfs_bd_sync(lfs);
525+
if (err) {
526+
if (err == LFS_ERR_CORRUPT) {
527+
goto relocate;
528+
}
529+
return err;
527530
}
528-
return err;
529-
}
530531

531-
// successful commit, check checksum to make sure
532-
crc = 0xffffffff;
533-
err = lfs_bd_crc(lfs, dir->pair[0], 0, 0x7fffffff & dir->d.size, &crc);
534-
if (err) {
535-
return err;
536-
}
532+
// successful commit, check checksum to make sure
533+
crc = 0xffffffff;
534+
err = lfs_bd_crc(lfs, dir->pair[0], 0,
535+
0x7fffffff & dir->d.size, &crc);
536+
if (err) {
537+
return err;
538+
}
537539

538-
if (crc == 0) {
539-
break;
540+
if (crc == 0) {
541+
break;
542+
}
540543
}
541544

542545
relocate:
@@ -554,7 +557,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
554557
}
555558

556559
// relocate half of pair
557-
err = lfs_alloc(lfs, &dir->pair[0]);
560+
int err = lfs_alloc(lfs, &dir->pair[0]);
558561
if (err) {
559562
return err;
560563
}
@@ -791,8 +794,6 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
791794
return err;
792795
}
793796
}
794-
795-
return 0;
796797
}
797798

798799

@@ -1021,7 +1022,7 @@ static int lfs_ctz_find(lfs_t *lfs,
10211022
lfs_block_t head, lfs_size_t size,
10221023
lfs_size_t pos, lfs_block_t *block, lfs_off_t *off) {
10231024
if (size == 0) {
1024-
*block = -1;
1025+
*block = 0xffffffff;
10251026
*off = 0;
10261027
return 0;
10271028
}
@@ -1053,79 +1054,84 @@ static int lfs_ctz_extend(lfs_t *lfs,
10531054
lfs_block_t head, lfs_size_t size,
10541055
lfs_off_t *block, lfs_block_t *off) {
10551056
while (true) {
1056-
// go ahead and grab a block
1057-
int err = lfs_alloc(lfs, block);
1058-
if (err) {
1059-
return err;
1060-
}
1061-
assert(*block >= 2 && *block <= lfs->cfg->block_count);
1057+
if (true) {
1058+
// go ahead and grab a block
1059+
int err = lfs_alloc(lfs, block);
1060+
if (err) {
1061+
return err;
1062+
}
1063+
assert(*block >= 2 && *block <= lfs->cfg->block_count);
10621064

1063-
err = lfs_bd_erase(lfs, *block);
1064-
if (err) {
1065-
if (err == LFS_ERR_CORRUPT) {
1066-
goto relocate;
1065+
err = lfs_bd_erase(lfs, *block);
1066+
if (err) {
1067+
if (err == LFS_ERR_CORRUPT) {
1068+
goto relocate;
1069+
}
1070+
return err;
10671071
}
1068-
return err;
1069-
}
10701072

1071-
if (size == 0) {
1072-
*off = 0;
1073-
return 0;
1074-
}
1073+
if (size == 0) {
1074+
*off = 0;
1075+
return 0;
1076+
}
10751077

1076-
size -= 1;
1077-
lfs_off_t index = lfs_ctz_index(lfs, &size);
1078-
size += 1;
1078+
size -= 1;
1079+
lfs_off_t index = lfs_ctz_index(lfs, &size);
1080+
size += 1;
10791081

1080-
// just copy out the last block if it is incomplete
1081-
if (size != lfs->cfg->block_size) {
1082-
for (lfs_off_t i = 0; i < size; i++) {
1083-
uint8_t data;
1084-
int err = lfs_cache_read(lfs, rcache, NULL, head, i, &data, 1);
1085-
if (err) {
1086-
return err;
1082+
// just copy out the last block if it is incomplete
1083+
if (size != lfs->cfg->block_size) {
1084+
for (lfs_off_t i = 0; i < size; i++) {
1085+
uint8_t data;
1086+
int err = lfs_cache_read(lfs, rcache, NULL,
1087+
head, i, &data, 1);
1088+
if (err) {
1089+
return err;
1090+
}
1091+
1092+
err = lfs_cache_prog(lfs, pcache, rcache,
1093+
*block, i, &data, 1);
1094+
if (err) {
1095+
if (err == LFS_ERR_CORRUPT) {
1096+
goto relocate;
1097+
}
1098+
return err;
1099+
}
10871100
}
10881101

1089-
err = lfs_cache_prog(lfs, pcache, rcache, *block, i, &data, 1);
1102+
*off = size;
1103+
return 0;
1104+
}
1105+
1106+
// append block
1107+
index += 1;
1108+
lfs_size_t skips = lfs_ctz(index) + 1;
1109+
1110+
for (lfs_off_t i = 0; i < skips; i++) {
1111+
int err = lfs_cache_prog(lfs, pcache, rcache,
1112+
*block, 4*i, &head, 4);
10901113
if (err) {
10911114
if (err == LFS_ERR_CORRUPT) {
10921115
goto relocate;
10931116
}
10941117
return err;
10951118
}
1096-
}
1097-
1098-
*off = size;
1099-
return 0;
1100-
}
1101-
1102-
// append block
1103-
index += 1;
1104-
lfs_size_t skips = lfs_ctz(index) + 1;
11051119

1106-
for (lfs_off_t i = 0; i < skips; i++) {
1107-
int err = lfs_cache_prog(lfs, pcache, rcache,
1108-
*block, 4*i, &head, 4);
1109-
if (err) {
1110-
if (err == LFS_ERR_CORRUPT) {
1111-
goto relocate;
1120+
if (i != skips-1) {
1121+
err = lfs_cache_read(lfs, rcache, NULL,
1122+
head, 4*i, &head, 4);
1123+
if (err) {
1124+
return err;
1125+
}
11121126
}
1113-
return err;
1114-
}
11151127

1116-
if (i != skips-1) {
1117-
err = lfs_cache_read(lfs, rcache, NULL, head, 4*i, &head, 4);
1118-
if (err) {
1119-
return err;
1120-
}
1128+
assert(head >= 2 && head <= lfs->cfg->block_count);
11211129
}
11221130

1123-
assert(head >= 2 && head <= lfs->cfg->block_count);
1131+
*off = 4*skips;
1132+
return 0;
11241133
}
11251134

1126-
*off = 4*skips;
1127-
return 0;
1128-
11291135
relocate:
11301136
LFS_DEBUG("Bad block at %d", *block);
11311137

@@ -1161,8 +1167,6 @@ static int lfs_ctz_traverse(lfs_t *lfs,
11611167

11621168
index -= 1;
11631169
}
1164-
1165-
return 0;
11661170
}
11671171

11681172

@@ -1200,7 +1204,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
12001204
entry.d.elen = sizeof(entry.d) - 4;
12011205
entry.d.alen = 0;
12021206
entry.d.nlen = strlen(path);
1203-
entry.d.u.file.head = -1;
1207+
entry.d.u.file.head = 0xffffffff;
12041208
entry.d.u.file.size = 0;
12051209
err = lfs_dir_append(lfs, &cwd, &entry, path);
12061210
if (err) {
@@ -1222,7 +1226,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
12221226
file->pos = 0;
12231227

12241228
if (flags & LFS_O_TRUNC) {
1225-
file->head = -1;
1229+
file->head = 0xffffffff;
12261230
file->size = 0;
12271231
}
12281232

@@ -1589,13 +1593,13 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
15891593
if (whence == LFS_SEEK_SET) {
15901594
file->pos = off;
15911595
} else if (whence == LFS_SEEK_CUR) {
1592-
if (-off > file->pos) {
1596+
if ((lfs_off_t)-off > file->pos) {
15931597
return LFS_ERR_INVAL;
15941598
}
15951599

15961600
file->pos = file->pos + off;
15971601
} else if (whence == LFS_SEEK_END) {
1598-
if (-off > file->size) {
1602+
if ((lfs_off_t)-off > file->size) {
15991603
return LFS_ERR_INVAL;
16001604
}
16011605

0 commit comments

Comments
 (0)