微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我在openssl源代码中注意到一个奇怪的成语,here并重复如下:
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;
        }
}

看来这段代码相当于:

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,!(passed_salt || in_noverify)) != 0)
                goto end;
        passwds[0] = passwd_malloc;
#else
        BIO_printf(bio_err,"password required\n");
        goto end;
#endif
}

我排除了一些解释:

>它可能是为passwds_static引入块范围,但是如果将为类似的目的提供封闭的范围
>它可能是一个通过几个有意义的变换变得没有意义的结构,但那个结构就是since the introduction of OPENSSL_NO_UI.

在这里遗漏了什么吗? (1)有什么好处?这是否用于其他代码库?

谢谢!

解决方法

看了其他类似的地方,I found an explanation
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;
}

在大多数情况下(包括其CI我猜),OPENSSL_NO_UI没有定义,所以两个分支都被编译.如果其中一个分支机构使用了更改,那么它将被编译器发现,它可以被修复,而无需测试所有的编译时交换机.

原文地址:https://www.jb51.cc/c/111569.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐