Skip to content

Commit

Permalink
Fix memory safety issues in string handlings (cf #79)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-uH57J9 committed Mar 25, 2024
1 parent 741c4be commit 24499a2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/physfs_archiver_csm.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,21 @@ static int csmLoadEntries(PHYSFS_Io *io, const PHYSFS_uint16 count, void *arc)
for (i = 0; i < count; i++)
{
PHYSFS_uint8 fn_len;
char name[12];
char name[12];
PHYSFS_uint32 size;
PHYSFS_uint32 pos;

BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &fn_len, 1), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 12), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, sizeof(name)), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);

if(fn_len > 12) fn_len = 12;
name[fn_len] = '\0'; /* name might not be null-terminated in file. */
if(fn_len > sizeof(name))
fn_len = sizeof(name);
if (fn_len > 0)
name[fn_len - 1] = '\0'; /* name might not be null-terminated in file. */
else
name[0] = '\0';
size = PHYSFS_swapULE32(size);
pos = PHYSFS_swapULE32(pos);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
Expand Down
2 changes: 2 additions & 0 deletions src/physfs_archiver_qpak.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static int qpakLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 56), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &pos, 4), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
/* name must be null terminated, so its length must leave space for null byte */
BAIL_IF_ERRPASS(strnlen(name, sizeof(name)) != sizeof(name), 0);
size = PHYSFS_swapULE32(size);
pos = PHYSFS_swapULE32(pos);
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
Expand Down
2 changes: 1 addition & 1 deletion src/physfs_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ PHYSFS_uint32 __PHYSFS_utf8codepoint(const char **_str)
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */
return UNICODE_BOGUS_CHAR_VALUE;

*_str += 6; /* skip to next possible start of codepoint. */
*_str += 5; /* skip to next possible start of codepoint. */
return UNICODE_BOGUS_CHAR_VALUE;
} /* else if */

Expand Down
6 changes: 6 additions & 0 deletions test/unit_unicode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Unit tests program for PhysicsFS's unicode functions.
*
* Please see the file LICENSE.txt in the source's root directory.
*/

0 comments on commit 24499a2

Please sign in to comment.