【发布时间】:2017-08-21 05:19:11
【问题描述】:
我注意到 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, 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
}
我排除了一些解释:
- 可能是为
passwds_static引入块作用域,但封闭的if将起到类似的作用 - 它可能是一个结构,通过几次有意义的转换变得毫无意义,但那个结构就在那里since the introduction of
OPENSSL_NO_UI。
我在这里遗漏了什么吗?这个if (1)有什么好处?这在其他代码库中使用吗?
谢谢!
【问题讨论】:
-
在我看来,它的唯一目的是为了避免和
#else子句出于任何原因 -
@CristiFati - 不。这与
#if 1无关,因为该构造根本没有出现在问题中。关于if(1)。 -
确实,这与
#if 0、#if 1或if (0)无关。 -
唯一明显的区别是单独使用预处理器不会产生关于不可访问代码的警告。但我非常怀疑这就是意图。
标签: c openssl idioms conditional-compilation