Skip to content

Commit

Permalink
trace: don't pretend success if it's not enabled
Browse files Browse the repository at this point in the history
Partially reverts d33d761 Don't fail when tracing is disabled

Commit d33d761 fixed the problem that the initialization of
libcrypto failed when tracing was disabled, because the unoperational
ossl_trace_init() function returned a failure code. The problem was
fixed by changing its return value from failure to success.

As part of the fix the return values of other unimplemented trace API
functions (like OSSL_trace_set_channel(),OSSL_trace_set_callback())
was changed from failure to success, too. This change was not necessary
and is a bit problematic IMHO, because nobody expects an unimplemented
function to pretend it succeeded.

It's the application's duty to handle the case correctly when the trace
API is not enabled (i.e., OPENSSL_NO_TRACE is defined), not the API's job
to pretend success just to prevent the application from failing.

Reviewed-by: Paul Dale <[email protected]>
(Merged from openssl#8552)
  • Loading branch information
mspncp committed Mar 29, 2019
1 parent 2e6b615 commit 0fda9f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
5 changes: 5 additions & 0 deletions apps/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ static char *make_config_name(void)
return p;
}


#ifndef OPENSSL_NO_TRACE
typedef struct tracedata_st {
BIO *bio;
unsigned int ingroup:1;
Expand Down Expand Up @@ -224,6 +226,7 @@ static void setup_trace(const char *str)
OPENSSL_free(val);
atexit(cleanup_trace);
}
#endif /* OPENSSL_NO_TRACE */

int main(int argc, char *argv[])
{
Expand Down Expand Up @@ -260,7 +263,9 @@ int main(int argc, char *argv[])
*/
atexit(destroy_prefix_method);

#ifndef OPENSSL_NO_TRACE
setup_trace(getenv("OPENSSL_TRACE"));
#endif

p = getenv("OPENSSL_DEBUG_MEMORY");
if (p != NULL && strcmp(p, "on") == 0)
Expand Down
32 changes: 12 additions & 20 deletions crypto/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,11 @@ void ossl_trace_cleanup(void)
int OSSL_trace_set_channel(int category, BIO *channel)
{
#ifndef OPENSSL_NO_TRACE
if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM
|| !set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
trace_attach_cb, trace_detach_cb))
return 0;
if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
return set_trace_data(category, SIMPLE_CHANNEL, &channel, NULL, NULL,
trace_attach_cb, trace_detach_cb))
#endif
return 1;
return 0;
}

#ifndef OPENSSL_NO_TRACE
Expand Down Expand Up @@ -367,7 +366,7 @@ int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
struct trace_data_st *trace_data = NULL;

if (category < 0 || category >= OSSL_TRACE_CATEGORY_NUM)
goto err;
return 0;

if (callback != NULL) {
if ((channel = BIO_new(&trace_method)) == NULL
Expand All @@ -386,41 +385,34 @@ int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data)
trace_attach_w_callback_cb, trace_detach_cb))
goto err;

goto done;
return 1;

err:
BIO_free(channel);
OPENSSL_free(trace_data);
return 0;
done:
#endif
return 1;

return 0;
}

int OSSL_trace_set_prefix(int category, const char *prefix)
{
int rv = 1;

#ifndef OPENSSL_NO_TRACE
if (category >= 0 || category < OSSL_TRACE_CATEGORY_NUM)
if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
return set_trace_data(category, 0, NULL, &prefix, NULL,
trace_attach_cb, trace_detach_cb);
rv = 0;
#endif
return rv;
return 0;
}

int OSSL_trace_set_suffix(int category, const char *suffix)
{
int rv = 1;

#ifndef OPENSSL_NO_TRACE
if (category >= 0 || category < OSSL_TRACE_CATEGORY_NUM)
if (category >= 0 && category < OSSL_TRACE_CATEGORY_NUM)
return set_trace_data(category, 0, NULL, NULL, &suffix,
trace_attach_cb, trace_detach_cb);
rv = 0;
#endif
return rv;
return 0;
}

#ifndef OPENSSL_NO_TRACE
Expand Down

0 comments on commit 0fda9f7

Please sign in to comment.