Skip to content

Commit

Permalink
chores: _new constructors now support err to be NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 2, 2023
1 parent 1236f53 commit a3e33a5
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 60 deletions.
14 changes: 10 additions & 4 deletions lib/archive/archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,20 @@ get_data_segment_size(const struct SqshSuperblock *superblock) {
struct SqshArchive *
sqsh_archive_new(
const void *source, const struct SqshConfig *config, int *err) {
int rv = 0;
struct SqshArchive *archive = calloc(1, sizeof(struct SqshArchive));
if (archive == NULL) {
return NULL;
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__archive_init(archive, source, config);
if (*err < 0) {
rv = sqsh__archive_init(archive, source, config);
if (rv < 0) {
free(archive);
return NULL;
archive = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return archive;
}
Expand Down
22 changes: 14 additions & 8 deletions lib/archive/compression_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,23 @@ compression_options(const struct SqshCompressionOptions *context) {

struct SqshCompressionOptions *
sqsh_compression_options_new(struct SqshArchive *sqsh, int *err) {
struct SqshCompressionOptions *context =
int rv = 0;
struct SqshCompressionOptions *compression_options =
calloc(1, sizeof(struct SqshCompressionOptions));
if (context == NULL) {
return NULL;
if (compression_options == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__compression_options_init(context, sqsh);
if (*err < 0) {
free(context);
return NULL;
rv = sqsh__compression_options_init(compression_options, sqsh);
if (rv < 0) {
free(compression_options);
compression_options = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return context;
return compression_options;
}

int
Expand Down
4 changes: 2 additions & 2 deletions lib/data/compression_options_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

struct SQSH_UNALIGNED SqshDataCompressionOptionsGzip {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/directory_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

struct SQSH_UNALIGNED SqshDataDirectoryEntry {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/fragment_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

struct SQSH_UNALIGNED SqshDataFragment {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/inode_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

struct SQSH_UNALIGNED SqshDataInodeDirectoryIndex {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/metablock_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

struct SQSH_UNALIGNED SqshDataMetablock {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/superblock_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

struct SQSH_UNALIGNED SqshDataSuperblock {
Expand Down
4 changes: 2 additions & 2 deletions lib/data/xattr_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include "../../include/sqsh_data_private.h"

#if defined(__FreeBSD__)
#include <sys/endian.h>
# include <sys/endian.h>
#else
#include <endian.h>
# include <endian.h>
#endif

#include <string.h>
Expand Down
18 changes: 12 additions & 6 deletions lib/directory/directory_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,21 @@ sqsh__directory_iterator_init(

struct SqshDirectoryIterator *
sqsh_directory_iterator_new(struct SqshInode *inode, int *err) {
int rv = 0;
struct SqshDirectoryIterator *iterator =
calloc(1, sizeof(struct SqshDirectoryIterator));
if (iterator == NULL) {
return NULL;
if (inode == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__directory_iterator_init(iterator, inode);
if (*err < 0) {
free(iterator);
return NULL;
rv = sqsh__directory_iterator_init(iterator, inode);
if (rv < 0) {
free(inode);
inode = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return iterator;
}
Expand Down
14 changes: 10 additions & 4 deletions lib/file/file_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,21 @@ sqsh__file_iterator_init(

struct SqshFileIterator *
sqsh_file_iterator_new(const struct SqshInode *inode, int *err) {
int rv = 0;
struct SqshFileIterator *iterator =
calloc(1, sizeof(struct SqshFileIterator));
if (iterator == NULL) {
return NULL;
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__file_iterator_init(iterator, inode);
if (*err < 0) {
rv = sqsh__file_iterator_init(iterator, inode);
if (rv < 0) {
free(iterator);
return NULL;
iterator = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return iterator;
}
Expand Down
22 changes: 14 additions & 8 deletions lib/file/file_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,22 @@ sqsh__file_reader_init(

struct SqshFileReader *
sqsh_file_reader_new(const struct SqshInode *inode, int *err) {
struct SqshFileReader *context = calloc(1, sizeof(struct SqshFileReader));
if (context == NULL) {
return NULL;
int rv = 0;
struct SqshFileReader *reader = calloc(1, sizeof(struct SqshFileReader));
if (reader == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__file_reader_init(context, inode);
if (*err < 0) {
free(context);
return NULL;
rv = sqsh__file_reader_init(reader, inode);
if (rv < 0) {
free(reader);
reader = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return context;
return reader;
}

int
Expand Down
22 changes: 14 additions & 8 deletions lib/inode/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,22 @@ sqsh__inode_init(

struct SqshInode *
sqsh_inode_new(struct SqshArchive *sqsh, uint64_t inode_ref, int *err) {
struct SqshInode *context = calloc(1, sizeof(struct SqshInode));
if (context == NULL) {
return NULL;
int rv = 0;
struct SqshInode *inode = calloc(1, sizeof(struct SqshInode));
if (inode == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__inode_init(context, sqsh, inode_ref);
if (*err < 0) {
free(context);
return NULL;
rv = sqsh__inode_init(inode, sqsh, inode_ref);
if (rv < 0) {
free(inode);
inode = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return context;
return inode;
}

bool
Expand Down
14 changes: 10 additions & 4 deletions lib/tree/walker.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,20 @@ sqsh__tree_walker_init(

struct SqshTreeWalker *
sqsh_tree_walker_new(struct SqshArchive *archive, int *err) {
int rv = 0;
struct SqshTreeWalker *walker = calloc(1, sizeof(struct SqshTreeWalker));
if (walker == NULL) {
return NULL;
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__tree_walker_init(walker, archive);
if (*err < 0) {
rv = sqsh__tree_walker_init(walker, archive);
if (rv < 0) {
free(walker);
return NULL;
walker = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return walker;
}
Expand Down
14 changes: 10 additions & 4 deletions lib/xattr/xattr_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,21 @@ sqsh__xattr_iterator_init(

struct SqshXattrIterator *
sqsh_xattr_iterator_new(const struct SqshInode *inode, int *err) {
int rv = 0;
struct SqshXattrIterator *iterator =
calloc(1, sizeof(struct SqshXattrIterator));
if (iterator == NULL) {
return NULL;
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;
}
*err = sqsh__xattr_iterator_init(iterator, inode);
if (*err < 0) {
rv = sqsh__xattr_iterator_init(iterator, inode);
if (rv < 0) {
free(iterator);
return NULL;
iterator = NULL;
}
out:
if (err != NULL) {
*err = rv;
}
return iterator;
}
Expand Down

0 comments on commit a3e33a5

Please sign in to comment.