Fix obscure bug with sorting zk-index buffer by size#64
Open
boyechko wants to merge 1 commit intolocalauthor:mainfrom
Open
Fix obscure bug with sorting zk-index buffer by size#64boyechko wants to merge 1 commit intolocalauthor:mainfrom
boyechko wants to merge 1 commit intolocalauthor:mainfrom
Conversation
The bug is rare, but if `zk-id-regexp` happens to match a portion of zk-index buffer that is *not* a zk-id, then `zk-index--current-id-list` will include it in its returned list, which in turn is used in `zk-index--current-file-list` to return a list of files. But since it's not a zk-id, `zk--parse-id` will return nil, resulting in `zk-index--current-file-list` returning a list with at least one nil element. This becomes a problem in `zk-index--sort-size`, since it uses `>` (greater) function to compare file sizes, which signals an error if an argument is nil. However, `file-attributes` (and `file-attribute-size`) can return `nil` if one is passed to them or if the file does not exist (see [Emacs Lisp manual about "File Attributes"][1]). Additionally, `zk-index--format-candidates` does not expect nils in the list of the files it works with, which again causes an error when `string-match` ends up being passed a nil rather than a string. The fix involves having 1) `zk-index--sort-size` ensure that it's always passing numbers to `>`, but since that just propagates the nil inside the list of files returned by `zk-index--sort`, we also make sure 2) `zk-index--format-candidates` does not pass nil to `string-match`. Another possibility would be to have `zk-index--current-file-list` remove nils from the file list it returns (replacing `files` with `(delq nil files)` as the final line), but that results in a small performance loss because `delq` needs to traverse the list looking for nils. I think my two-pronged fix is more robust, anyway, since it deals with the errors at the level where they occur (in the calls to `>` and `string-match`). Of course, we can also wrap those calls in `ignore-errors`, but that would make debugging more difficult in the future. [1]: https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Attributes.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug is rare, but if
zk-id-regexphappens to match a portion of zk-index buffer that is not a zk-id, thenzk-index--current-id-listwill include it in its returned list, which in turn is used inzk-index--current-file-listto return a list of files. But since it's not a zk-id,zk--parse-idwill return nil, resulting inzk-index--current-file-listreturning a list with at least one nil element. This becomes a problem inzk-index--sort-size, since it uses>(greater) function to compare file sizes, which signals an error if an argument is nil. However,file-attributes(andfile-attribute-size) can returnnilif one is passed to them or if the file does not exist (see Emacs Lisp manual about "File Attributes").Additionally,
zk-index--format-candidatesdoes not expect nils in the list of the files it works with, which again causes an error whenstring-matchends up being passed a nil rather than a string.The fix involves having 1)
zk-index--sort-sizeensure that it's always passing numbers to>, but since that just propagates the nil inside the list of files returned byzk-index--sort, we also make sure 2)zk-index--format-candidatesdoes not pass nil tostring-match.Another possibility would be to have
zk-index--current-file-listremove nils from the file list it returns (replacingfileswith(delq nil files)as the final line), but that results in a small performance loss becausedelqneeds to traverse the list looking for nils. I think my two-pronged fix is more robust, anyway, since it deals with the errors at the level where they occur (in the calls to>andstring-match). Of course, we can also wrap those calls inignore-errors, but that might make debugging more difficult in the future.