这个C成语是什么(if(1))?

Éti*_*lon 9 c openssl idioms conditional-compilation

我在openssl源代码中注意到一个奇怪的习惯用语,在这里重复如下:

if ((in == NULL) && (passwds == NULL)) {
        if (1) {                                    (* <---- HERE *)
#ifndef OPENSSL_NO_UI
            /* build a null-terminated list */
            static char *passwds_static[2] = { NULL, NULL };

            passwds = passwds_static;
            if (in == NULL)
                if (EVP_read_pw_string
                    (passwd_malloc, passwd_malloc_size, "Password: ",
                     !(passed_salt || in_noverify)) != 0)
                    goto end;
            passwds[0] = passwd_malloc;
        } else {
#endif
            BIO_printf(bio_err, "password required\n");
            goto end;
        }
}
Run Code Online (Sandbox Code Playgroud)

似乎这段代码相当于:

if ((in == NULL) && (passwds == NULL)) {
#ifndef OPENSSL_NO_UI
        /* build a null-terminated list */
        static char *passwds_static[2] = { NULL, NULL };

        passwds = passwds_static;
        if (in == NULL)
            if (EVP_read_pw_string
                (passwd_malloc, passwd_malloc_size, "Password: ",
                 !(passed_salt || in_noverify)) != 0)
                goto end;
        passwds[0] = passwd_malloc;
#else
        BIO_printf(bio_err, "password required\n");
        goto end;
#endif
}
Run Code Online (Sandbox Code Playgroud)

我排除了一些解释:

  • 它可能是为了引入块范围passwds_static,但封闭if将起到类似的作用
  • 它可能是一个通过几个有意义的转换变得毫无意义的构造,但是自从引入以来OPENSSL_NO_UI,该构造就存在.

我在这里错过了什么吗?这有什么好处if (1)?这是否用于其他代码库?

谢谢!

Éti*_*lon 10

看了其他类似的地方后,我找到了一个解释:

if (1) { /* This is a trick we use to avoid bit rot.
          * at least the "else" part will always be
          * compiled.
          */
#ifdef AF_INET6
    family = AF_INET6;
} else {
#endif
    BIOerr(BIO_F_ACPT_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
    goto exit_loop;
}
Run Code Online (Sandbox Code Playgroud)

在大多数情况下(包括他们的CI我猜),OPENSSL_NO_UI没有定义,因此编译了两个分支.如果其中一个分支使用了更改,它将被编译器发现,并且可以修复,而无需测试所有编译时开关.