Skip to content

Commit e8354e1

Browse files
committed
Create replacement DFD before mutating texture in HDR 4x4 relabel path
The UASTC HDR 4x4 to ASTC HDR 4x4 early exit in ktxTexture2_TranscodeBasis freed the texture's DFD and relabelled vkFormat before calling vk2dfd(). If vk2dfd() returned NULL the function returned an error while leaving the texture with a freed pDfd and an already-changed vkFormat. Allocate the replacement DFD first and only then modify the texture, so failure leaves it untouched. No behavior change on the success path.
1 parent 02f39de commit e8354e1

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

lib/src/basis_transcode.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,16 @@ ktx2transcoderFormat(ktx_transcode_fmt_e ktx_fmt) {
203203
// Early exit for redundant transcode from UASTC4x4 to ASTC4x4
204204
if (colorModel == KHR_DF_MODEL_UASTC_HDR_4x4 &&
205205
outputFormat == KTX_TTF_ASTC_HDR_4x4_RGBA) {
206+
// Create the new DFD before modifying the texture so a failure
207+
// leaves the texture unchanged instead of freeing its DFD and
208+
// relabelling its vkFormat first.
209+
uint32_t* newDfd = vk2dfd(VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK);
210+
if (!newDfd)
211+
return KTX_OUT_OF_MEMORY;
206212
// Fix up the current texture
207213
free(This->pDfd);
214+
This->pDfd = newDfd;
208215
This->vkFormat = VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK;
209-
This->pDfd = vk2dfd(VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK);
210-
if (!This->pDfd)
211-
return KTX_INVALID_VALUE; // Format is unknown or unsupported.
212216
return KTX_SUCCESS;
213217
}
214218

0 commit comments

Comments
 (0)