Skip to content

Commit

Permalink
Fix gcc warnings in appendLibId. (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCallow authored Sep 4, 2022
1 parent 9bd2f9b commit 895799d
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/writer2.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ appendLibId(ktxHashList* head, ktxHashListEntry* writerEntry)
const char* libVer;
const char libIdIntro[] = " / libktx ";
size_t idLen, libIdLen;

if (writerEntry) {
ktx_uint32_t len;
result = ktxHashListEntry_GetValue(writerEntry, &len, (void**)&id);
Expand All @@ -123,25 +124,22 @@ appendLibId(ktxHashList* head, ktxHashListEntry* writerEntry)
id = "Unidentified app";
idLen = 17;
}

// strnstr needed because KTXwriter values may not be NUL terminated.
if (strnstr(id, "__default__", idLen) != NULL) {
libVer = STR(LIBKTX_DEFAULT_VERSION);
} else {
libVer = STR(LIBKTX_VERSION);
}
// sizeof(libIdIntro) includes space for the terminating NUL which we will
// sizeof(libIdIntro) includes space for its terminating NUL which we will
// overwrite so no need for +1 after strlen.
libIdLen = sizeof(libIdIntro) + (ktx_uint32_t)strlen(libVer);
char* libId = malloc(libIdLen);
if (!libId)
return KTX_OUT_OF_MEMORY;
// &libIdIntro[0] instead of libIdIntro is to workaround a gcc warning
// that I'm passing the same thing to sizeof as to the src
// parameter (i.e. I'm requesting the sizeof a pointer).
// Actually libIdIntro is an array of char not a pointer. Looks
// like a gcc bug.
strncpy(libId, &libIdIntro[0], sizeof(libIdIntro));
strncpy(&libId[sizeof(libIdIntro)-1], libVer, strlen(libVer) + 1);
strncpy(libId, libIdIntro, libIdLen);
strncpy(&libId[sizeof(libIdIntro)-1], libVer,
libIdLen-(sizeof(libIdIntro)-1));

if (strnstr(id, libId, idLen) != NULL) {
// This lib id is already in the writer value.
Expand Down

0 comments on commit 895799d

Please sign in to comment.