Skip to content

Commit

Permalink
[inc/fdb_low_lvl] fix An error occurred in FDB_ALIGN(size, align) whe…
Browse files Browse the repository at this point in the history
…n align is not 2 to the nth power (#233)
  • Loading branch information
DarryZh authored Aug 17, 2023
1 parent fab8a16 commit 3e441f6
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion inc/fdb_low_lvl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
* would return 16.
*/
#define FDB_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
#define FDB_ALIGN(size, align) ((size + align - 1) - ((size + align -1) % align))
/* align by write granularity */
#define FDB_WG_ALIGN(size) (FDB_ALIGN(size, (FDB_WRITE_GRAN + 7)/8))
/**
Expand Down

7 comments on commit 3e441f6

@hsSam
Copy link

@hsSam hsSam commented on 3e441f6 Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个更新后我原来正常使用的程序初始化后,读取数据报错了。

�[32;22m[I/FAL] Flash Abstraction Layer (V0.5.99) initialize success.�[0m
[FlashDB] FlashDB V2.0.0 is initialize success.
[FlashDB] You can get the latest version on https://github.com/armink/FlashDB .
fdb_kvdb_init: 0
[FlashDB][kv][env][kvdb] Error: The KV (@0x00000014) CRC32 check failed!

@guoweilkd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit cause macro expansion errors, stm32f405 demo will be stuck in CRC32 check. I use the following code to workground

#define FDB_ALIGN(size, align)                    ((size + align - 1) - ((size + align -1) % align))
/* align by write granularity */
#define FDB_WG_ALIGN(size)                        (FDB_ALIGN(size, ((FDB_WRITE_GRAN + 7)/8)))

@armink
Copy link
Owner

@armink armink commented on 3e441f6 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hsSam @guoweilkd 你们的 FDB_WRITE_GRAN 都是配置的多大的呀?

@guoweilkd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我用的就是stm32f405的demo, FDB_WRITE_GRAN = 8, 直接跑demo会陷入到crc死循环中,具体代码在create_kv_blob()函数中的如下位置:

static fdb_err_t create_kv_blob(fdb_kvdb_t db, kv_sec_info_t sector, const char *key, const void *value, size_t len)
{
    ...
    kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, &kv_hdr.name_len, sizeof(uint32_t));
    kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, &kv_hdr.value_len, sizeof(uint32_t));
    kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, key, kv_hdr.name_len);
    align_remain = FDB_WG_ALIGN(kv_hdr.name_len) - kv_hdr.name_len; // name_len = 10时, 宏展开变成了9, 9 - 10 成了一个负数,造成如下死循环
    while (align_remain--) {
        kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, &ff, 1);
    }
    kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, value, kv_hdr.value_len);
    align_remain = FDB_WG_ALIGN(kv_hdr.value_len) - kv_hdr.value_len;
    while (align_remain--) {
        kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, &ff, 1);
    }
    ....
}

@armink
Copy link
Owner

@armink armink commented on 3e441f6 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guoweilkd FDB_WG_ALIGN(10) 在 FDB_WRITE_GRAN = 8 时,结果应该是 10 吧

FDB_WG_ALIGN(10) = FDB_ALIGN(10, 1) = (10 + 1 - 1) - ((10 + 1 - 1) % 1) = 10 - (10 % 1) = 10 - 0 = 10

模拟器测试下,结果也是 10

image

你方便方便把整个宏展开步骤拆一下嘛?看看具体哪个环节出的问题

@guoweilkd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你测试的不是最新代码?最新代码的这两个宏是

#define FDB_ALIGN(size, align)                    ((size + align - 1) - ((size + align -1) % align))
/* align by write granularity */
#define FDB_WG_ALIGN(size)                        (FDB_ALIGN(size, (FDB_WRITE_GRAN + 7)/8))

后面的 ((size + align -1) % align)展开后%/的完整性会被打乱
FDB_WG_ALIGN(10)的展开结果如下: 10 + 0 % 15 / 8 = 10 /8 = 1, (size + align - 1) = 10, 所以 ((size + align - 1) - ((size + align -1) % align)) = 9

@armink
Copy link
Owner

@armink armink commented on 3e441f6 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 感谢反馈,确实是这个问题,给 入参 都加上括号就没问题了,类似下面这样

image

方便的话,可以帮忙提交个 PR 修正下哈

Please sign in to comment.