/* Close stream, which also flushes the buffer */
ret = bt_close_memstream(buf, &size, out_fp);
+ /*
+ * See fclose(3). Further access to out_fp after both success
+ * and error, even through another bt_close_memstream(), results
+ * in undefined behavior. Nullify out_fp to ensure we don't
+ * fclose it twice on error.
+ */
+ out_fp = NULL;
if (ret < 0) {
BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p",
strerror(errno), mdec);
BT_LOGD("Metadata stream is plain text: mdec-addr=%p", mdec);
+ if (init_pos < 0) {
+ BT_LOGE_ERRNO("Failed to get current file position", ".");
+ status = CTF_METADATA_DECODER_STATUS_ERROR;
+ goto end;
+ }
+
/* Check text-only metadata header and version */
nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
if (nr_items < 2) {