Skip to content

Commit b8e4433

Browse files
authored
Merge pull request #1052 from wangdongustc/assert_null_sync
Assert on NULL IO functions
2 parents 215613e + dae656a commit b8e4433

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lfs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,6 +4217,14 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
42174217
// which littlefs currently does not support
42184218
LFS_ASSERT((bool)0x80000000);
42194219

4220+
// check that the required io functions are provided
4221+
LFS_ASSERT(lfs->cfg->read != NULL);
4222+
#ifndef LFS_READONLY
4223+
LFS_ASSERT(lfs->cfg->prog != NULL);
4224+
LFS_ASSERT(lfs->cfg->erase != NULL);
4225+
LFS_ASSERT(lfs->cfg->sync != NULL);
4226+
#endif
4227+
42204228
// validate that the lfs-cfg sizes were initiated properly before
42214229
// performing any arithmetic logics with them
42224230
LFS_ASSERT(lfs->cfg->read_size != 0);

scripts/prettyasserts.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def write_header(f, limit=LIMIT):
8686
f.writeln("}")
8787
f.writeln()
8888
f.writeln("__attribute__((unused))")
89+
f.writeln("static void __pretty_assert_print_ptr(")
90+
f.writeln(" const void *v, size_t size) {")
91+
f.writeln(" (void)size;")
92+
f.writeln(" printf(\"%p\", v);")
93+
f.writeln("}")
94+
f.writeln()
95+
f.writeln("__attribute__((unused))")
8996
f.writeln("static void __pretty_assert_print_mem(")
9097
f.writeln(" const void *v, size_t size) {")
9198
f.writeln(" const uint8_t *v_ = v;")
@@ -183,6 +190,23 @@ def write_header(f, limit=LIMIT):
183190
f.writeln(" _rh, strlen(_rh)); \\")
184191
f.writeln(" } \\")
185192
f.writeln("} while (0)")
193+
for op, cmp in sorted(CMP.items()):
194+
# Only EQ and NE are supported when compared to NULL.
195+
if cmp not in ['eq', 'ne']:
196+
continue
197+
f.writeln("#define __PRETTY_ASSERT_PTR_%s(lh, rh) do { \\"
198+
% cmp.upper())
199+
f.writeln(" const void *_lh = (const void*)(uintptr_t)lh; \\")
200+
f.writeln(" const void *_rh = (const void*)(uintptr_t)rh; \\")
201+
f.writeln(" if (!(_lh %s _rh)) { \\" % op)
202+
f.writeln(" __pretty_assert_fail( \\")
203+
f.writeln(" __FILE__, __LINE__, \\")
204+
f.writeln(" __pretty_assert_print_ptr, \"%s\", \\"
205+
% cmp)
206+
f.writeln(" (const void*){_lh}, 0, \\")
207+
f.writeln(" (const void*){_rh}, 0); \\")
208+
f.writeln(" } \\")
209+
f.writeln("} while (0)")
186210
f.writeln()
187211
f.writeln()
188212

@@ -301,6 +325,8 @@ def p_assert(p):
301325
cmp = p.expect('cmp') ; p.accept('ws')
302326
rh = p_expr(p) ; p.accept('ws')
303327
p.expect(')')
328+
if rh == 'NULL' or lh == 'NULL':
329+
return mkassert('ptr', CMP[cmp], lh, rh)
304330
return mkassert('int', CMP[cmp], lh, rh)
305331
except ParseFailure:
306332
p.pop(state)

0 commit comments

Comments
 (0)