Skip to content

Commit

Permalink
CID 1524378: possibly incorrect parameter validation and removal of s…
Browse files Browse the repository at this point in the history
…ome unused/duplicate code.
  • Loading branch information
ni4 authored and antonsviridenko committed Jan 23, 2024
1 parent 8c8f81a commit e058546
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 69 deletions.
26 changes: 4 additions & 22 deletions src/librepgp/stream-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ rnp_result_t
read_mem_src(pgp_source_t *src, pgp_source_t *readsrc)
{
pgp_dest_t dst;
rnp_result_t ret;
rnp_result_t ret = RNP_ERROR_GENERIC;
size_t sz = 0;

if ((ret = init_mem_dest(&dst, NULL, 0))) {
return ret;
Expand All @@ -573,32 +574,13 @@ read_mem_src(pgp_source_t *src, pgp_source_t *readsrc)
goto done;
}

if ((ret = init_mem_src(src, mem_dest_own_memory(&dst), dst.writeb, true))) {
goto done;
}

ret = RNP_SUCCESS;
sz = dst.writeb;
ret = init_mem_src(src, mem_dest_own_memory(&dst), sz, true);
done:
dst_close(&dst, true);
return ret;
}

rnp_result_t
file_to_mem_src(pgp_source_t *src, const char *filename)
{
pgp_source_t fsrc = {};
rnp_result_t res = RNP_ERROR_GENERIC;

if ((res = init_file_src(&fsrc, filename))) {
return res;
}

res = read_mem_src(src, &fsrc);
fsrc.close();

return res;
}

const void *
mem_src_get_memory(pgp_source_t *src, bool own)
{
Expand Down
7 changes: 0 additions & 7 deletions src/librepgp/stream-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,6 @@ rnp_result_t init_null_src(pgp_source_t *src);
**/
rnp_result_t read_mem_src(pgp_source_t *src, pgp_source_t *readsrc);

/** @brief init memory source with contents of the specified file
* @param src pre-allocated source structure
* @param filename name of the file
* @return RNP_SUCCESS or error code
**/
rnp_result_t file_to_mem_src(pgp_source_t *src, const char *filename);

/** @brief get memory from the memory source
* @param src initialized memory source
* @param own transfer ownership of the memory
Expand Down
42 changes: 2 additions & 40 deletions src/tests/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,21 +749,6 @@ TEST_F(rnp_tests, test_ffi_add_userid)
rnp_ffi_destroy(ffi);
}

static bool
file_equals(const char *filename, const void *data, size_t len)
{
pgp_source_t msrc = {};
bool res = false;

if (file_to_mem_src(&msrc, filename)) {
return false;
}

res = (msrc.size == len) && !memcmp(mem_src_get_memory(&msrc), data, len);
msrc.close();
return res;
}

static void
test_ffi_init_sign_file_input(rnp_input_t *input, rnp_output_t *output)
{
Expand Down Expand Up @@ -893,29 +878,6 @@ test_ffi_check_signatures(rnp_op_verify_t *verify)
rnp_buffer_destroy(hname);
}

static bool
test_ffi_check_recovered()
{
pgp_source_t msrc1 = {};
pgp_source_t msrc2 = {};
bool res = false;

if (file_to_mem_src(&msrc1, "recovered")) {
return false;
}

if (file_to_mem_src(&msrc2, "plaintext")) {
goto finish;
}

res = (msrc1.size == msrc2.size) &&
!memcmp(mem_src_get_memory(&msrc1), mem_src_get_memory(&msrc2), msrc1.size);
finish:
msrc1.close();
msrc2.close();
return res;
}

TEST_F(rnp_tests, test_ffi_signatures_memory)
{
rnp_ffi_t ffi = NULL;
Expand Down Expand Up @@ -1025,7 +987,7 @@ TEST_F(rnp_tests, test_ffi_signatures)
output = NULL;
assert_rnp_success(rnp_ffi_destroy(ffi));
// check output
assert_true(test_ffi_check_recovered());
assert_true(file_to_vec("recovered") == file_to_vec("plaintext"));
}

TEST_F(rnp_tests, test_ffi_signatures_detached_memory)
Expand Down Expand Up @@ -3652,7 +3614,7 @@ TEST_F(rnp_tests, test_ffi_aead_params)
rnp_output_destroy(output);
output = NULL;
// compare the decrypted file
assert_true(file_equals("decrypted", plaintext, strlen(plaintext)));
assert_string_equal(file_to_str("decrypted").c_str(), plaintext);
rnp_unlink("decrypted");

// final cleanup
Expand Down

0 comments on commit e058546

Please sign in to comment.