|
42 | 42 | // StartComAndWoSignData.inc |
43 | 43 | #include "StartComAndWoSignData.inc" |
44 | 44 |
|
| 45 | +#include <algorithm> |
45 | 46 | #include <errno.h> |
46 | 47 | #include <limits.h> // INT_MAX |
47 | 48 | #include <math.h> |
48 | 49 | #include <stdlib.h> |
49 | 50 | #include <string.h> |
| 51 | +#include <vector> |
50 | 52 |
|
51 | 53 | #define THROW_AND_RETURN_IF_NOT_BUFFER(val, prefix) \ |
52 | 54 | do { \ |
@@ -421,44 +423,33 @@ void ThrowCryptoError(Environment* env, |
421 | 423 | Local<Value> exception_v = Exception::Error(message); |
422 | 424 | CHECK(!exception_v.IsEmpty()); |
423 | 425 | Local<Object> exception = exception_v.As<Object>(); |
424 | | - ERR_STATE* es = ERR_get_state(); |
425 | | - |
426 | | - if (es->bottom != es->top) { |
427 | | - Local<Array> error_stack = Array::New(env->isolate()); |
428 | | - int top = es->top; |
429 | | - |
430 | | - // Build the error_stack array to be added to opensslErrorStack property. |
431 | | - for (unsigned int i = 0; es->bottom != es->top;) { |
432 | | - unsigned long err_buf = es->err_buffer[es->top]; // NOLINT(runtime/int) |
433 | | - // Only add error string if there is valid err_buffer. |
434 | | - if (err_buf) { |
435 | | - char tmp_str[256]; |
436 | | - ERR_error_string_n(err_buf, tmp_str, sizeof(tmp_str)); |
437 | | - error_stack->Set(env->context(), i, |
438 | | - String::NewFromUtf8(env->isolate(), tmp_str, |
439 | | - v8::NewStringType::kNormal) |
440 | | - .ToLocalChecked()).FromJust(); |
441 | | - // Only increment if we added to error_stack. |
442 | | - i++; |
443 | | - } |
444 | 426 |
|
445 | | - // Since the ERR_STATE is a ring buffer, we need to use modular |
446 | | - // arithmetic to loop back around in the case where bottom is after top. |
447 | | - // Using ERR_NUM_ERRORS macro defined in openssl. |
448 | | - es->top = (((es->top - 1) % ERR_NUM_ERRORS) + ERR_NUM_ERRORS) % |
449 | | - ERR_NUM_ERRORS; |
| 427 | + std::vector<Local<String>> errors; |
| 428 | + for (;;) { |
| 429 | + unsigned long err = ERR_get_error(); // NOLINT(runtime/int) |
| 430 | + if (err == 0) { |
| 431 | + break; |
450 | 432 | } |
451 | | - |
452 | | - // Restore top. |
453 | | - es->top = top; |
454 | | - |
455 | | - // Add the opensslErrorStack property to the exception object. |
456 | | - // The new property will look like the following: |
457 | | - // opensslErrorStack: [ |
458 | | - // 'error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib', |
459 | | - // 'error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 err' |
460 | | - // ] |
461 | | - exception->Set(env->context(), env->openssl_error_stack(), error_stack) |
| 433 | + char tmp_str[256]; |
| 434 | + ERR_error_string_n(err, tmp_str, sizeof(tmp_str)); |
| 435 | + errors.push_back(String::NewFromUtf8(env->isolate(), tmp_str, |
| 436 | + v8::NewStringType::kNormal) |
| 437 | + .ToLocalChecked()); |
| 438 | + } |
| 439 | + |
| 440 | + // ERR_get_error returns errors in order of most specific to least |
| 441 | + // specific. We wish to have the reverse ordering: |
| 442 | + // opensslErrorStack: [ |
| 443 | + // 'error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib', |
| 444 | + // 'error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 err' |
| 445 | + // ] |
| 446 | + if (!errors.empty()) { |
| 447 | + std::reverse(errors.begin(), errors.end()); |
| 448 | + Local<Array> errors_array = Array::New(env->isolate(), errors.size()); |
| 449 | + for (size_t i = 0; i < errors.size(); i++) { |
| 450 | + errors_array->Set(env->context(), i, errors[i]).FromJust(); |
| 451 | + } |
| 452 | + exception->Set(env->context(), env->openssl_error_stack(), errors_array) |
462 | 453 | .FromJust(); |
463 | 454 | } |
464 | 455 |
|
|
0 commit comments