cnpy.h::npy_save: fix bug when nels = 0 and data = NULL#87
Open
tdp2110 wants to merge 1 commit intorogersce:masterfrom
Open
cnpy.h::npy_save: fix bug when nels = 0 and data = NULL#87tdp2110 wants to merge 1 commit intorogersce:masterfrom
tdp2110 wants to merge 1 commit intorogersce:masterfrom
Conversation
When nels = 0, no bytes from `const T* data` are written to the npy file (line 219: fwrite(data,sizeof(T),nels,fp)). Therefore, the crc of the to-be-written data only needs to be computed for the numpy header, as done on line 171. It might be thought that `crc32(initial_crc, data_ptr, data_size)` would always return `initial_crc` if `data_size = 0`. This is true for every value of data_ptr *except* NULL. In this case crc32 returns 0, *independent* of initial_crc. The result of calling npz_save with a shape of total size 0 (i.e., the product of all elements in the shape) should not depend on whether `data` is a null pointer or not. Fix this bug by only updating the crc when nels != 0, independent of the value of `data`.
|
Reference for crc32: https://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/zlib-crc32-1.html. In the Description section, "initial checksum" refers to zero. See the source code (https://github.com/madler/zlib/blob/master/crc32.c#L626) |
pkestene
added a commit
to pkestene/cnpy-cmake
that referenced
this pull request
Apr 25, 2024
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.
When nels = 0, no bytes from
const T* dataare written to the npyfile (line 219: fwrite(data,sizeof(T),nels,fp)).
Therefore, the crc of the to-be-written data only needs to be
computed for the numpy header, as done on line 171.
It might be thought that
crc32(initial_crc, data_ptr, data_size)would always return
initial_crcifdata_size = 0. This is truefor every value of data_ptr except NULL. In this case crc32
returns 0, independent of initial_crc.
The result of calling npz_save with a shape of total size 0
(i.e., the product of all elements in the shape) should not depend
on whether
datais a null pointer or not.Fix this bug by only updating the crc when nels != 0, independent
of the value of
data.