Skip to content

Commit b8a0551

Browse files
committed
Use local variable to store partially-calculated size.
This avoid re-entrancy problems, such as if this is ever interrupted mid-execution, and called a second time before the initial execution completes.
1 parent 4ea74d4 commit b8a0551

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/usb/uf2/ghostfat.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,37 @@ static FAT_BootBlock const BootBlock = {
123123
static uint32_t current_flash_size(void)
124124
{
125125
static uint32_t flash_sz = 0;
126+
uint32_t result = flash_sz; // presumes atomic 32-bit read/write and static result
126127

127128
// only need to compute once
128-
if ( flash_sz == 0 )
129+
if ( result == 0 )
129130
{
130131
// return 1 block of 256 bytes
131132
if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) )
132133
{
133-
flash_sz = 256;
134+
result = 256;
134135
}else
135136
{
136137
bootloader_settings_t const * boot_setting;
137138
bootloader_util_settings_get(&boot_setting);
138139

139-
flash_sz = boot_setting->bank_0_size;
140+
result = boot_setting->bank_0_size;
140141

141142
// Copy size must be multiple of 256 bytes
142143
// else we will got an issue copying current.uf2
143-
if (flash_sz & 0xff)
144+
if (result & 0xff)
144145
{
145-
flash_sz = (flash_sz & ~0xff) + 256;
146+
result = (result & ~0xff) + 256;
146147
}
147148

148149
// if bank0 size is not valid, happens when flashed with jlink
149150
// use maximum application size
150-
if ( (flash_sz == 0) || (flash_sz == 0xFFFFFFFFUL) )
151+
if ( (result == 0) || (result == 0xFFFFFFFFUL) )
151152
{
152-
flash_sz = FLASH_SIZE;
153+
result = FLASH_SIZE;
153154
}
154155
}
156+
flash_sz = result; // presumes atomic 32-bit read/write and static result
155157
}
156158

157159
return flash_sz;

0 commit comments

Comments
 (0)