Avoid mismatched comparison (suggested code improvement, not a bug!)#1114
Avoid mismatched comparison (suggested code improvement, not a bug!)#1114nmouha wants to merge 9 commits into
Conversation
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
|
Hi @nmouha, Thank you for this! More than fixing the particular instance, I'd be interested in how to automatically detect this in the future. How did you find this? @mkannwischer and I were looking into using things like CodeQL or ast-grep for deeper code-analysis, but we haven't converged onto anything yet. |
|
(Same comment as in the other PR for mlkem-native) I think it's a good idea to flag comparisons with mismatched type, but I'm not convinced we have to make all types equal at the point of declaration. Instead, it seems to me that in a comparison with different types, one should down-cast the larger type to the smaller explicitly -- this would a) trigger CBMC to check that the cast is safe, b) require -- although we don't have a mechanical way to enforce this yet -- some // Safety: ... prose. |
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
|
Thank you, @hanno-becker, for the quick reply! Of course I'm happy to explain how I found these instances. I did use CodeQL, here's my If you were considering to maybe use CodeQL, it's easy to enable it in GitHub and to add custom queries. I wrote about this here: https://mouha.be/sha-3-buffer-overflow-part-2/ However, I don't think there is an easy way to modify the CodeQL query to avoid the false positives in If you are maybe considering a code change to avoid these false positives, then I'm suggesting |
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
Signed-off-by: Nicky Mouha <nmouha@users.noreply.github.com>
|
Converting this to draft due to the CI failures, sorry about that! I will propose similar changes to mldsa-native once the PR for mlkem-native has converged: pq-code-package/mlkem-native#1690 |
I searched for all comparisons where the left and right hand sides have different types.
If we exclude cases where either side is a constant (e.g.,
inlen > 0corresponds tosize_t > int), then there are only five results.Four results occur in
fips202.candfips202x4.c, and all of them involve upcastingrfromunsigned inttosize_t. So this can never result in an issue, even when verification is turned off.The fifth result occurs in
ct.h, where the comparisoni < lencorresponds tounsigned int < size_t. I am suggesting to avoid this mismatched comparison by declaringiassize_tinstead ofunsigned.This is not a bug, but a potential issue if
mld_ct_memcmpis used to replacememcmpin an unrelated project where verification is turned off (otherwise the precondition onlenwill detect any issues).I am suggesting my proposed change not as a bug fix but as a code improvement, as it may benefit projects that reuse
mld_ct_memcmpwithout verification.