Skip to content

Commit

Permalink
file: add default implementations for inode types
Browse files Browse the repository at this point in the history
This change adds default implementations for all inode getters that
where just a NULL-pointer beforehand. This hardens the library against
misusage and allows to gracefully return error codes and very large
values that would trigger errors if used instead of segfaulting.
  • Loading branch information
Gottox committed Dec 6, 2023
1 parent de34387 commit 82a0940
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 77 deletions.
57 changes: 47 additions & 10 deletions libsqsh/include/sqsh_file_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,36 +281,73 @@ SQSH_NO_EXPORT int sqsh__file_cleanup(struct SqshFile *context);
* file/inode_directory.c
*/

extern const struct SqshInodeImpl sqsh__inode_directory_impl;
extern const struct SqshInodeImpl sqsh__inode_directory_ext_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_directory_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_directory_ext_impl;

/***************************************
* file/inode_file.c
*/

extern const struct SqshInodeImpl sqsh__inode_file_impl;
extern const struct SqshInodeImpl sqsh__inode_file_ext_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_file_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_file_ext_impl;

/***************************************
* file/inode_symlink.c
*/

extern const struct SqshInodeImpl sqsh__inode_symlink_impl;
extern const struct SqshInodeImpl sqsh__inode_symlink_ext_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_symlink_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_symlink_ext_impl;

/***************************************
* file/inode_device.c
*/

extern const struct SqshInodeImpl sqsh__inode_device_impl;
extern const struct SqshInodeImpl sqsh__inode_device_ext_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_device_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_device_ext_impl;

/***************************************
* file/inode_ipc.c
*/

extern const struct SqshInodeImpl sqsh__inode_ipc_impl;
extern const struct SqshInodeImpl sqsh__inode_ipc_ext_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_ipc_impl;
SQSH_NO_EXPORT extern const struct SqshInodeImpl sqsh__inode_ipc_ext_impl;

/***************************************
* file/inode_null.c
*/

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_directory_block_start(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint16_t
sqsh__file_inode_null_directory_block_offset(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_directory_parent_inode(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint64_t
sqsh__file_inode_null_blocks_start(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint32_t sqsh__file_inode_null_block_size_info(
const struct SqshDataInode *inode, sqsh_index_t index);

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_fragment_block_index(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_fragment_block_offset(const struct SqshDataInode *inode);

SQSH_NO_EXPORT const char *
sqsh__file_inode_null_symlink_target_path(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_symlink_target_size(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_device_id(const struct SqshDataInode *inode);

SQSH_NO_EXPORT uint32_t
sqsh__file_inode_null_xattr_index(const struct SqshDataInode *inode);

#ifdef __cplusplus
}
Expand Down
8 changes: 2 additions & 6 deletions libsqsh/src/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ sqsh_open_by_ref(struct SqshArchive *sqsh, uint64_t inode_ref, int *err) {

bool
sqsh_file_is_extended(const struct SqshFile *context) {
return context->impl->xattr_index != NULL;
return context->impl->xattr_index != sqsh__file_inode_null_xattr_index;
}

uint32_t
Expand Down Expand Up @@ -370,11 +370,7 @@ sqsh_file_inode_ref(const struct SqshFile *context) {

uint32_t
sqsh_file_xattr_index(const struct SqshFile *context) {
if (context->impl->xattr_index) {
return context->impl->xattr_index(get_inode(context));
} else {
return SQSH_INODE_NO_XATTR;
}
return context->impl->xattr_index(get_inode(context));
}

int
Expand Down
34 changes: 17 additions & 17 deletions libsqsh/src/file/inode_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ const struct SqshInodeImpl sqsh__inode_device_impl = {
.hard_link_count = inode_device_hard_link_count,
.size = inode_device_file_size,

.blocks_start = NULL,
.block_size_info = NULL,
.fragment_block_index = NULL,
.fragment_block_offset = NULL,
.blocks_start = sqsh__file_inode_null_blocks_start,
.block_size_info = sqsh__file_inode_null_block_size_info,
.fragment_block_index = sqsh__file_inode_null_fragment_block_index,
.fragment_block_offset = sqsh__file_inode_null_fragment_block_offset,

.directory_block_start = NULL,
.directory_block_offset = NULL,
.directory_parent_inode = NULL,
.directory_block_start = sqsh__file_inode_null_directory_block_start,
.directory_block_offset = sqsh__file_inode_null_directory_block_offset,
.directory_parent_inode = sqsh__file_inode_null_directory_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = inode_device_id,

.xattr_index = NULL,
.xattr_index = sqsh__file_inode_null_xattr_index,
};

const struct SqshInodeImpl sqsh__inode_device_ext_impl = {
Expand All @@ -116,16 +116,16 @@ const struct SqshInodeImpl sqsh__inode_device_ext_impl = {
.hard_link_count = inode_device_ext_hard_link_count,
.size = inode_device_file_size,

.blocks_start = NULL,
.block_size_info = NULL,
.fragment_block_index = NULL,
.fragment_block_offset = NULL,
.blocks_start = sqsh__file_inode_null_blocks_start,
.block_size_info = sqsh__file_inode_null_block_size_info,
.fragment_block_index = sqsh__file_inode_null_fragment_block_index,
.fragment_block_offset = sqsh__file_inode_null_fragment_block_offset,

.directory_block_start = NULL,
.directory_block_offset = NULL,
.directory_parent_inode = NULL,
.directory_block_start = sqsh__file_inode_null_directory_block_start,
.directory_block_offset = sqsh__file_inode_null_directory_block_offset,
.directory_parent_inode = sqsh__file_inode_null_directory_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = inode_device_ext_id,

Expand Down
23 changes: 17 additions & 6 deletions libsqsh/src/file/inode_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,20 @@ const struct SqshInodeImpl sqsh__inode_directory_impl = {
.hard_link_count = inode_directory_hard_link_count,
.size = inode_directory_size,

.blocks_start = sqsh__file_inode_null_blocks_start,
.block_size_info = sqsh__file_inode_null_block_size_info,
.fragment_block_index = sqsh__file_inode_null_fragment_block_index,
.fragment_block_offset = sqsh__file_inode_null_fragment_block_offset,

.directory_block_start = inode_directory_block_start,
.directory_block_offset = inode_directory_block_offset,
.directory_parent_inode = inode_directory_parent_inode,

.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = sqsh__file_inode_null_device_id,

.xattr_index = sqsh__file_inode_null_xattr_index,
};

const struct SqshInodeImpl sqsh__inode_directory_ext_impl = {
Expand All @@ -143,18 +154,18 @@ const struct SqshInodeImpl sqsh__inode_directory_ext_impl = {
.hard_link_count = inode_directory_ext_hard_link_count,
.size = inode_directory_ext_size,

.blocks_start = NULL,
.block_size_info = NULL,
.fragment_block_index = NULL,
.fragment_block_offset = NULL,
.blocks_start = sqsh__file_inode_null_blocks_start,
.block_size_info = sqsh__file_inode_null_block_size_info,
.fragment_block_index = sqsh__file_inode_null_fragment_block_index,
.fragment_block_offset = sqsh__file_inode_null_fragment_block_offset,

.directory_block_start = inode_directory_ext_block_start,
.directory_block_offset = inode_directory_ext_block_offset,
.directory_parent_inode = inode_directory_ext_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = NULL,
.device_id = sqsh__file_inode_null_device_id,

.xattr_index = inode_directory_ext_xattr_index,
};
22 changes: 11 additions & 11 deletions libsqsh/src/file/inode_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ const struct SqshInodeImpl sqsh__inode_file_impl = {
.fragment_block_index = inode_file_fragment_block_index,
.fragment_block_offset = inode_file_fragment_block_offset,

.directory_block_start = NULL,
.directory_block_offset = NULL,
.directory_parent_inode = NULL,
.directory_block_start = sqsh__file_inode_null_directory_block_start,
.directory_block_offset = sqsh__file_inode_null_directory_block_offset,
.directory_parent_inode = sqsh__file_inode_null_directory_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = NULL,
.device_id = sqsh__file_inode_null_device_id,

.xattr_index = NULL,
.xattr_index = sqsh__file_inode_null_xattr_index,
};

const struct SqshInodeImpl sqsh__inode_file_ext_impl = {
Expand All @@ -193,13 +193,13 @@ const struct SqshInodeImpl sqsh__inode_file_ext_impl = {
.fragment_block_index = inode_file_ext_fragment_block_index,
.fragment_block_offset = inode_file_ext_fragment_block_offset,

.directory_block_start = NULL,
.directory_block_offset = NULL,
.directory_parent_inode = NULL,
.directory_block_start = sqsh__file_inode_null_directory_block_start,
.directory_block_offset = sqsh__file_inode_null_directory_block_offset,
.directory_parent_inode = sqsh__file_inode_null_directory_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = NULL,
.device_id = sqsh__file_inode_null_device_id,

.xattr_index = inode_file_ext_xattr_index,
};
38 changes: 19 additions & 19 deletions libsqsh/src/file/inode_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ const struct SqshInodeImpl sqsh__inode_ipc_impl = {
.hard_link_count = inode_ipc_hard_link_count,
.size = inode_ipc_size,

.blocks_start = NULL,
.block_size_info = NULL,
.fragment_block_index = NULL,
.fragment_block_offset = NULL,
.blocks_start = sqsh__file_inode_null_blocks_start,
.block_size_info = sqsh__file_inode_null_block_size_info,
.fragment_block_index = sqsh__file_inode_null_fragment_block_index,
.fragment_block_offset = sqsh__file_inode_null_fragment_block_offset,

.directory_block_start = NULL,
.directory_block_offset = NULL,
.directory_parent_inode = NULL,
.directory_block_start = sqsh__file_inode_null_directory_block_start,
.directory_block_offset = sqsh__file_inode_null_directory_block_offset,
.directory_parent_inode = sqsh__file_inode_null_directory_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = NULL,
.device_id = sqsh__file_inode_null_device_id,

.xattr_index = NULL,
.xattr_index = sqsh__file_inode_null_xattr_index,
};

const struct SqshInodeImpl sqsh__inode_ipc_ext_impl = {
Expand All @@ -102,18 +102,18 @@ const struct SqshInodeImpl sqsh__inode_ipc_ext_impl = {
.hard_link_count = inode_ipc_ext_hard_link_count,
.size = inode_ipc_size,

.blocks_start = NULL,
.block_size_info = NULL,
.fragment_block_index = NULL,
.fragment_block_offset = NULL,
.blocks_start = sqsh__file_inode_null_blocks_start,
.block_size_info = sqsh__file_inode_null_block_size_info,
.fragment_block_index = sqsh__file_inode_null_fragment_block_index,
.fragment_block_offset = sqsh__file_inode_null_fragment_block_offset,

.directory_block_start = NULL,
.directory_block_offset = NULL,
.directory_parent_inode = NULL,
.directory_block_start = sqsh__file_inode_null_directory_block_start,
.directory_block_offset = sqsh__file_inode_null_directory_block_offset,
.directory_parent_inode = sqsh__file_inode_null_directory_parent_inode,

.symlink_target_path = NULL,
.symlink_target_path = sqsh__file_inode_null_symlink_target_path,

.device_id = NULL,
.device_id = sqsh__file_inode_null_device_id,

.xattr_index = inode_ipc_ext_xattr_index,
};
Loading

0 comments on commit 82a0940

Please sign in to comment.