Skip to content

Commit fe26f86

Browse files
authored
Merge pull request #3388 from jimklimov/issue-3302-logs-comments
Update debug trace logs and code comments
2 parents da58012 + b198324 commit fe26f86

File tree

12 files changed

+574
-93
lines changed

12 files changed

+574
-93
lines changed

clients/nutclient.cpp

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,60 @@ SSL_CTX* Socket::_ssl_ctx = nullptr;
332332
# endif /* WITH_OPENSSL */
333333

334334
# ifdef WITH_NSS
335-
static void nss_error(const char* funcname)
336-
{
337-
char buffer[256];
338-
PRInt32 length = PR_GetErrorText(buffer);
339-
if (length > 0 && length < 256) {
340-
std::cerr << "nss_error " << static_cast<long>(PR_GetError()) << " in " << funcname << " : " << buffer << std::endl;
335+
/** Detail the currently raised NSS error code if possible, and debug-log
336+
* it with caller-provided text (typically the calling function name). */
337+
static void nss_error(const char* text)
338+
{
339+
std::string err_name_buf;
340+
PRErrorCode err_num = PR_GetError();
341+
const char *err_name = PR_ErrorToName(err_num);
342+
PRInt32 err_len = PR_GetErrorTextLength();
343+
344+
if (err_name) {
345+
err_name_buf << " (" << err_name << ")";
346+
}
347+
348+
if (err_len > 0) {
349+
char *buffer = (char *)calloc(err_len + 1, sizeof(char));
350+
if (buffer) {
351+
PR_GetErrorText(buffer);
352+
std::cerr << "nss_error "
353+
<< static_cast<long>(err_num)
354+
<< err_name_buf
355+
<< " in " << text
356+
<< " : "
357+
<< buffer
358+
<< std::endl;
359+
free(buffer);
360+
} else {
361+
std::cerr << "nss_error "
362+
<< static_cast<long>(err_num)
363+
<< err_name_buf
364+
<< " in " << text
365+
<< " : "
366+
<< "Failed to allocate internal error buffer "
367+
<< "for detailed error text, needs "
368+
<< static_cast<long>(err_len) << " bytes"
369+
<< std::endl;
370+
}
341371
} else {
342-
std::cerr << "nss_error " << static_cast<long>(PR_GetError()) << " in " << funcname << std::endl;
372+
/* The code above may be obsolete or not ubiquitous, try another way */
373+
const char *err_text = PR_ErrorToString(err_num, PR_LANGUAGE_I_DEFAULT);
374+
if (err_text && *err_text) {
375+
std::cerr << "nss_error "
376+
<< static_cast<long>(err_num)
377+
<< err_name_buf
378+
<< " in " << text
379+
<< " : "
380+
<< err_text
381+
<< std::endl;
382+
} else {
383+
std::cerr << "nss_error "
384+
<< static_cast<long>(err_num)
385+
<< err_name_buf
386+
<< " in " << text
387+
<< std::endl;
388+
}
343389
}
344390
}
345391

0 commit comments

Comments
 (0)