@@ -604,20 +604,33 @@ mem_src_get_memory(pgp_source_t *src, bool own)
604604 return param->memory ;
605605}
606606
607- bool
608- init_dst_common (pgp_dest_t *dst, size_t paramsize)
607+ pgp_dest_t ::pgp_dest_t (size_t paramsize)
609608{
610- memset (dst, 0 , sizeof (*dst));
611- dst->werr = RNP_SUCCESS;
609+ werr = RNP_SUCCESS;
612610 if (!paramsize) {
613- return true ;
611+ return ;
614612 }
615613 /* allocate param */
616- dst->param = calloc (1 , paramsize);
617- if (!dst->param ) {
614+ param = calloc (1 , paramsize);
615+ if (!param) {
616+ /* LCOV_EXCL_START */
618617 RNP_LOG (" allocation failed" );
618+ throw rnp::rnp_exception (RNP_ERROR_OUT_OF_MEMORY);
619+ /* LCOV_EXCL_END */
619620 }
620- return dst->param ;
621+ }
622+
623+ // pgp_dest_t constructor do the same job, but we keep this function to preserve api
624+ bool
625+ init_dst_common (pgp_dest_t *dst, size_t paramsize)
626+ {
627+ try {
628+ *dst = pgp_dest_t (paramsize);
629+ } catch (const std::exception &e) {
630+ return false ; // LCOV_EXCL_LINE
631+ }
632+
633+ return true ;
621634}
622635
623636void
@@ -626,26 +639,26 @@ dst_write(pgp_dest_t *dst, const void *buf, size_t len)
626639 /* we call write function only if all previous calls succeeded */
627640 if ((len > 0 ) && (dst->write ) && (dst->werr == RNP_SUCCESS)) {
628641 /* if cache non-empty and len will overflow it then fill it and write out */
629- if ((dst->clen > 0 ) && (dst->clen + len > sizeof ( dst->cache ))) {
630- memcpy (dst->cache + dst->clen , buf, sizeof ( dst->cache ) - dst->clen );
631- buf = (uint8_t *) buf + sizeof ( dst->cache ) - dst->clen ;
632- len -= sizeof ( dst->cache ) - dst->clen ;
633- dst->werr = dst->write (dst, dst->cache , sizeof ( dst->cache ));
634- dst->writeb += sizeof ( dst->cache );
642+ if ((dst->clen > 0 ) && (dst->clen + len > dst->cache . size ( ))) {
643+ memcpy (dst->cache . data () + dst->clen , buf, dst->cache . size ( ) - dst->clen );
644+ buf = (uint8_t *) buf + dst->cache . size ( ) - dst->clen ;
645+ len -= dst->cache . size ( ) - dst->clen ;
646+ dst->werr = dst->write (dst, dst->cache . data (), dst->cache . size ( ));
647+ dst->writeb += dst->cache . size ( );
635648 dst->clen = 0 ;
636649 if (dst->werr != RNP_SUCCESS) {
637650 return ;
638651 }
639652 }
640653
641654 /* here everything will fit into the cache or cache is empty */
642- if (dst->no_cache || (len > sizeof ( dst->cache ))) {
655+ if (dst->no_cache || (len > dst->cache . size ( ))) {
643656 dst->werr = dst->write (dst, buf, len);
644657 if (!dst->werr ) {
645658 dst->writeb += len;
646659 }
647660 } else {
648- memcpy (dst->cache + dst->clen , buf, len);
661+ memcpy (dst->cache . data () + dst->clen , buf, len);
649662 dst->clen += len;
650663 }
651664 }
673686dst_flush (pgp_dest_t *dst)
674687{
675688 if ((dst->clen > 0 ) && (dst->write ) && (dst->werr == RNP_SUCCESS)) {
676- dst->werr = dst->write (dst, dst->cache , dst->clen );
689+ dst->werr = dst->write (dst, dst->cache . data () , dst->clen );
677690 dst->writeb += dst->clen ;
678691 dst->clen = 0 ;
679692 }
@@ -759,11 +772,9 @@ file_dst_close(pgp_dest_t *dst, bool discard)
759772static rnp_result_t
760773init_fd_dest (pgp_dest_t *dst, int fd, const char *path)
761774{
762- if (!init_dst_common (dst, 0 )) {
763- return RNP_ERROR_OUT_OF_MEMORY;
764- }
765-
766775 try {
776+ *dst = pgp_dest_t (0 );
777+
767778 std::unique_ptr<pgp_dest_file_param_t > param (new pgp_dest_file_param_t ());
768779 param->path = path;
769780 param->fd = fd;
@@ -1009,8 +1020,10 @@ init_mem_dest(pgp_dest_t *dst, void *mem, unsigned len)
10091020{
10101021 pgp_dest_mem_param_t *param;
10111022
1012- if (!init_dst_common (dst, sizeof (*param))) {
1013- return RNP_ERROR_OUT_OF_MEMORY;
1023+ try {
1024+ *dst = pgp_dest_t (sizeof (*param));
1025+ } catch (const std::exception &e) {
1026+ return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
10141027 }
10151028
10161029 param = (pgp_dest_mem_param_t *) dst->param ;
0 commit comments