aperezdc/ngx-fancyindex

File size sort broken on files with size > INT_MAX (2GB)

shric opened this issue · 3 comments

shric commented

In ngx_http_fancyindex_cmp_entries_size_{desc,asc} you have:

return (int) (second->size - first->size); but size is an off_t. Converting down to int breaks the comparison for large files vs. small files. Suggest something like this instead:

ngx_http_fancyindex_cmp_entries_size_desc:
  return (second->size > first->size) - (second->size < first->size);
ngx_http_fancyindex_cmp_entries_size_asc:
  return (first->size > second->size) - (first->size < second->size);

The conversion to int is needed because it's the return type of the comparison function; but what can be done here instead is using comparison operators, as follows:

/* ngx_http_fancyindex_cmp_entries_size_asc */
if (first->size < second->size)
  return -1;
if (first->size > second->size)
  return 1;
return 0;

I'll make a patch for this soon. Thanks for reporting the issue!

shric commented

I suggested comparison operators in my original report:

return (first->size > second->size) - (first->size < second->size);

This is equivalent to what you suggested, but far more terse. I agree that yours is far more readable, but mine is kind of idiomatic as it's common to implement this for comparison functions.: http://www.gnu.org/software/libc/manual/html_node/Comparison-Functions.html

Anyway, thanks for replying and patching!

@shric Oh, you are right indeed! I've read your example too fast 😇