Skip to content

Commit

Permalink
Merge pull request #93 from Samdal/gs_util_get_file_extension-size-check
Browse files Browse the repository at this point in the history
Update gs.h gs_util_get_file_extension
  • Loading branch information
MrFrenik authored Jan 8, 2024
2 parents 6d32fbb + 30a0085 commit 9b9cc81
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions gs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1167,23 +1167,15 @@ void gs_util_get_file_extension
const char* file_path
)
{
uint32_t str_len = gs_string_length(file_path);
const char* at = (file_path + str_len - 1);
while (*at != '.' && at != file_path)
{
at--;
}

if (*at == '.')
{
at++;
uint32_t i = 0;
while (*at)
{
char c = *at;
buffer[i++] = *at++;
}
buffer[i] = '\0';
// assumes that buffer and buffer_size is non-zero
char* extension = strrchr(file_path, '.');
if (extension) {
uint32_t extension_len = strlen(extension+1);
uint32_t len = (extension_len >= buffer_size) ? buffer_size - 1 : extension_len;
memcpy(buffer, extension+1, len);
buffer[len] = '\0';
} else {
buffer[0] = '\0';
}
}

Expand Down

0 comments on commit 9b9cc81

Please sign in to comment.