Replies: 1 comment 1 reply
-
Something like that was done in the past, but caused issues and had to be reverted. Pointers and arrays are not interchangeable. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
libsodium started from nacl, which had the unique selling point of having a very clear API with self-explanatory function prototypes. Example from their docs:
You can't really do anything wrong here. Here is what the libsodium prototype declaration looks like:
If you look at the implementation, you will find:
The compiler has no idea how large the destination buffer needs to be, and thus can't warn if it is too small. The declaration should instead look like this:
int crypto_box_keypair(unsigned char pk[crypto_box_PUBLICKEYBYTES], unsigned char sk[crypto_box_SECRETKEYBYTES]);
Going further, many libsodium functions take pointer + length style arguments. Those can be annotated as well. Here is how you'd do it with gcc:
Unfortunately this annotation only works in recent-ish gcc versions, not in clang.
Use #if __has_attribute(access) to guard against breakage.
The upside of doing this is that the compiler will tell you if you call things wrong, for example if you call a function on 100 bytes from a buffer that is only 50 bytes long. Also, if you tell gcc that you are planning to read from a buffer, it can warn if the buffer is not initialized.
Beta Was this translation helpful? Give feedback.
All reactions