Skip to content

fix(coverimage): fix operator precedence and parameter usage in cache functions#15032

Merged
Frenzie merged 2 commits into
koreader:masterfrom
msk:fix/coverimage-cleanCache-bugs
Feb 23, 2026
Merged

fix(coverimage): fix operator precedence and parameter usage in cache functions#15032
Frenzie merged 2 commits into
koreader:masterfrom
msk:fix/coverimage-cleanCache-bugs

Conversation

@msk

@msk msk commented Feb 23, 2026

Copy link
Copy Markdown
Contributor
  1. cleanCache(): Fix operator precedence in while loop condition. In Lua, and has higher precedence than or, so the bounds check index <= #files was only applied to the cache size condition, not the file count condition. Added parentheses to ensure the bounds check applies to both conditions.

  2. getCacheFiles(): Use the function parameters cache_path and cache_prefix consistently instead of mixing them with self.cover_image_cache_path and self.cover_image_cache_prefix.


This change is Reviewable

… functions

Fix two bugs in the coverimage plugin:

1. cleanCache(): Fix operator precedence in while loop condition. In Lua,
   `and` has higher precedence than `or`, so the bounds check `index <= #files`
   was only applied to the cache size condition, not the file count condition.
   Added parentheses to ensure the bounds check applies to both conditions.

2. getCacheFiles(): Use the function parameters `cache_path` and `cache_prefix`
   consistently instead of mixing them with `self.cover_image_cache_path` and
   `self.cover_image_cache_prefix`.
Comment thread plugins/coverimage.koplugin/main.lua Outdated
or (cache_size > self.cover_image_cache_maxsize * 1000 * 1000 and self.cover_image_cache_maxsize ~= 0)
while ((cache_count > self.cover_image_cache_maxfiles and self.cover_image_cache_maxfiles ~= 0)
or (cache_size > self.cover_image_cache_maxsize * 1000 * 1000 and self.cover_image_cache_maxsize ~= 0))
and index <= #files do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If it's supposed to apply to both I'd put it first.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Swapped two conditions in 6bd3da9.

Move `index <= #files` to the beginning of the while condition for clarity
and to benefit from short-circuit evaluation.
@Frenzie Frenzie requested a review from poire-z February 23, 2026 21:36

@poire-z poire-z left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fine with me. Not familiar at all with this plugin, but the changes looks logical.

Comment on lines +314 to 317
while index <= #files
and ((cache_count > self.cover_image_cache_maxfiles and self.cover_image_cache_maxfiles ~= 0)
or (cache_size > self.cover_image_cache_maxsize * 1000 * 1000 and self.cover_image_cache_maxsize ~= 0)) do
os.remove(files[index].name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(I would indent more the and and or lines, so they visually look more like a continuation of the while line, than just operation in the while branch like os.remove() - but that's just me :))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It was that way already, so I'll just leave it be.

@Frenzie Frenzie merged commit ccd0a80 into koreader:master Feb 23, 2026
4 checks passed
@Frenzie Frenzie added the Plugin label Feb 23, 2026
@Frenzie Frenzie added this to the 2026.02 milestone Feb 23, 2026
@msk msk deleted the fix/coverimage-cleanCache-bugs branch February 23, 2026 21:59
0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
… functions (koreader#15032)

Fix two bugs in the coverimage plugin:

1. cleanCache(): Fix operator precedence in while loop condition. In Lua,
   `and` has higher precedence than `or`, so the bounds check `index <= #files`
   was only applied to the cache size condition, not the file count condition.
   Added parentheses to ensure the bounds check applies to both conditions.

2. getCacheFiles(): Use the function parameters `cache_path` and `cache_prefix`
   consistently instead of mixing them with `self.cover_image_cache_path` and
   `self.cover_image_cache_prefix`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants