@@ -1070,6 +1070,16 @@ static struct wc_error_queue* wc_current_node;
10701070/* heap info currently used for allocation of entries */
10711071static void * wc_error_heap ;
10721072
1073+ /* Copies of the file and reason strings of the last node peeked or pulled.
1074+ * Pointers returned to the caller point here rather than into a node, so a
1075+ * remove or clear from another thread after the mutex is released can not
1076+ * leave the caller with a pointer to freed memory. The contents are only valid
1077+ * until the next peek or pull by the same thread. Only written with the mutex
1078+ * held and the last byte is never written, so the strings are always
1079+ * terminated. */
1080+ static THREAD_LS_T char wc_errq_file [WOLFSSL_MAX_ERROR_SZ ];
1081+ static THREAD_LS_T char wc_errq_reason [WOLFSSL_MAX_ERROR_SZ ];
1082+
10731083/* mutex for list operation protection */
10741084static wolfSSL_Mutex wc_error_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE (wc_error_mutex );
10751085#define ERRQ_MUTEX_INIT () wc_InitMutex(&wc_error_mutex)
@@ -1109,6 +1119,21 @@ int wc_LoggingCleanup(void)
11091119 return 0 ;
11101120}
11111121
1122+ /* Copy the node strings asked for to the static buffers and return those.
1123+ * Must be called with the mutex held. */
1124+ static void exportErrorNodeStrings (const struct wc_error_queue * err ,
1125+ const char * * file , const char * * reason )
1126+ {
1127+ if (file != NULL ) {
1128+ XMEMCPY (wc_errq_file , err -> file , WOLFSSL_MAX_ERROR_SZ - 1 );
1129+ * file = wc_errq_file ;
1130+ }
1131+ if (reason != NULL ) {
1132+ XMEMCPY (wc_errq_reason , err -> error , WOLFSSL_MAX_ERROR_SZ - 1 );
1133+ * reason = wc_errq_reason ;
1134+ }
1135+ }
1136+
11121137static int peekErrorNode (int idx , const char * * file , const char * * reason ,
11131138 int * line )
11141139{
@@ -1135,12 +1160,7 @@ static int peekErrorNode(int idx, const char **file, const char **reason,
11351160 return BAD_STATE_E ;
11361161 }
11371162
1138- if (file != NULL ) {
1139- * file = err -> file ;
1140- }
1141- if (reason != NULL ) {
1142- * reason = err -> error ;
1143- }
1163+ exportErrorNodeStrings (err , file , reason );
11441164 if (line != NULL ) {
11451165 * line = err -> line ;
11461166 }
@@ -1153,8 +1173,10 @@ static int peekErrorNode(int idx, const char **file, const char **reason,
11531173 * idx : if -1 then the most recent node is looked at,
11541174 * otherwise search through queue for node at the given index starting
11551175 * from the absolute head wc_errors
1156- * file : pointer to internal file string
1157- * reason : pointer to internal error reason
1176+ * file : pointer to a copy of the file string, valid until the next peek or
1177+ * pull
1178+ * reason : pointer to a copy of the error reason, valid until the next peek
1179+ * or pull
11581180 * line : line number that error happened at
11591181 *
11601182 * Returns a negative value in error case, on success returns the nodes error
@@ -1185,12 +1207,7 @@ static int pullErrorNode(const char **file, const char **reason, int *line)
11851207 return BAD_STATE_E ;
11861208 }
11871209
1188- if (file != NULL ) {
1189- * file = err -> file ;
1190- }
1191- if (reason != NULL ) {
1192- * reason = err -> error ;
1193- }
1210+ exportErrorNodeStrings (err , file , reason );
11941211 if (line != NULL ) {
11951212 * line = err -> line ;
11961213 }
@@ -1201,8 +1218,8 @@ static int pullErrorNode(const char **file, const char **reason, int *line)
12011218}
12021219
12031220/* Pulls the current node from error queue and increments current state.
1204- * Note: this does not delete nodes because input arguments are pointing to
1205- * node buffers .
1221+ * Note: this does not delete nodes. The file and reason pointers returned are
1222+ * to copies that are valid until the next peek or pull .
12061223 *
12071224 * file pointer to file that error was in. Can be NULL to return no file.
12081225 * reason error string giving reason for error. Can be NULL to return no reason.
0 commit comments