From: geraldo netto <[email protected]>
Committer: Waldemar Kozaczuk <[email protected]>
Branch: master

test: fix possible undefined behaviour at the loop that fills the buffer for crypt tests

this patch avoids a possible undefined behviour at the loop when it tries to access the password field after the 6th position (password is a fixed size string of 6 chars) /home/netto/Desktop/osv/osv-tomcat9/tests/tst-crypt.c:44:29: warning: iteration 6 invokes undefined behavior [-Waggressive-loop-optimizations]
    buf[i * 8 + j] = password[i] >> j & 1;
                     ~~~~~~~~^~~
/home/netto/Desktop/osv/osv-tomcat9/tests/tst-crypt.c:42:2: note: within this loop
  for (i = 0; i < 8; i++) {
  ^~~

Signed-off-by: geraldo netto <[email protected]>
Message-Id: <[email protected]>

---
diff --git a/tests/tst-crypt.c b/tests/tst-crypt.c
--- a/tests/tst-crypt.c
+++ b/tests/tst-crypt.c
@@ -32,41 +32,37 @@ int main(void) {
if (strcmp(result3, crypt(password, concat_str((char*) "$5$", password))) != 0) return -3;

// $6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1 - if (strcmp(result4, crypt(password, concat_str((char*) "$6$", password))) !=0) return -4; + if (strcmp(result4, crypt(password, concat_str((char*) "$6$", password))) != 0) return -4;

        // encrypt
        // http://man7.org/linux/man-pages/man3/encrypt.3.html
        char key[64];
        char buf[64];
-       char txt[9];
+       char txt[6];
        int i, j;

-       for (i = 0; i < 8; i++) {
+       for (i = 0; i < 6; i++) {
                for (j = 0; j < 8; j++) {
                        buf[i * 8 + j] = password[i] >> j & 1;
                }
-
-               setkey(key);
        }
+       setkey(key);
+

        encrypt(buf, 0);
-       for (i = 0; i < 8; i++) {
+       for (i = 0; i < 6; i++) {
                for (j = 0, txt[i] = '\0'; j < 8; j++) {
                        txt[i] |= buf[i * 8 + j] << j;
                }
-
-               txt[8] = '\0';
        }

        if (strcmp(password, txt) == 0) return -5;

        encrypt(buf, 1);
-       for (i = 0; i < 8; i++) {
+       for (i = 0; i < 6; i++) {
                for (j = 0, txt[i] = '\0'; j < 8; j++) {
                        txt[i] |= buf[i * 8 + j] << j;
                }
-
-               txt[8] = '\0';
        }

        return (strcmp(password, txt) == 0) ? 0 : -6;

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to