I noticed a strange idiom in openssl source code, here and repeated below:
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;
}
}
It seems that this piece of code is equivalent to:
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
}
I ruled out some explanations:
- it could be to introduce block scope for
passwds_static
, but the enclosingif
would serve a similar purpose - it could be a construct that through several meaningful transformations becomes meaningless, but that construct is there since the introduction of
OPENSSL_NO_UI
.
Am I missing something here? What are the advantages of this if (1)
? Is this used in other code bases?
Thanks!
#else
clause for whatever reason – Zabrze#if 1
, since that construct doesn't appear at all in the question. It's aboutif(1)
. – Zabrze#if 0
,#if 1
, orif (0)
. – TarrantOPENSSL_NO_UI
is not defined,in_noverify
is reported as not used. That happens both in theif (1)
and#else
cases so it does not seem to be the reason. – Tarrantif(1)
) gives a way to test the entire function which several branches. However, the second solution might require configuration changes to the test engine which in many cases is complicated. Just to summarize my point of view, the approach is good to focus unit testing on code level, but not on configuration level. – Impostorif (1) {..}
is present, maybe they will want to change the default behavior in the future (wo needing to recompile w/woOPENSSL_NO_UI
). – She