Re: [PATCH v2 5/5] cli: lazily create the crypto gpg context only when needed

Subject: Re: [PATCH v2 5/5] cli: lazily create the crypto gpg context only when needed

Date: Fri, 18 May 2012 12:45:08 -0700

To: Austin Clements

Cc: Notmuch Mail

From: Jameson Graef Rollins


On Fri, May 18 2012, Austin Clements <amdragon@MIT.EDU> wrote:
>> +    /* Lazily create the gpgctx if it's needed and hasn't been initialized yet */
>> +    if ((GMIME_IS_MULTIPART_ENCRYPTED (part) || GMIME_IS_MULTIPART_SIGNED (part))
>> +	&& (node->ctx->crypto->verify || node->ctx->crypto->decrypt)) {
>> +	if (!node->ctx->crypto->gpgctx) {
>
> These if conditions could be combined, like
>
>     if ((GMIME_IS_MULTIPART_ENCRYPTED (part) || GMIME_IS_MULTIPART_SIGNED (part))
> 	&& (node->ctx->crypto->verify || node->ctx->crypto->decrypt)
> 	&& !node->ctx->crypto->gpgctx) {
>
> When I see two nested 'if's like this, I expect there to be an else
> part to the inner if or something after the inner if (why else would
> it be separate?) and then I wind up matching braces when I don't see
> anything.  Also, one 'if' would save a level of indentation.

To explain what I was explaining on IRC, and what I should have noted in
the original patch, I did this on purpose because I'm looking forward to
the S/MIME support I was trying to get working.

gmime 2.6 provides a separate context for pkcs7 (S/MIME).  In this
context initialization section we will therefore have to test for
initialization of the relevant context.  Since I knew that going into
this I decided to anticipate it by constructing things this way now
future diffs wouldn't have to include the indentation of this section
and could therefore be cleaner and smaller.  If folks have issue with
that explanation let me know.

> Perhaps "If crypto->gpgctx is NULL, it will be lazily initialized."?
> The variable does have to be "initialized", in the sense that it can't
> be uninitialized data.

That sounds like a better wording.  I'll fix.

> It's slightly awkward that it's the caller's responsibility to free
> this lazily constructed object.  That should probably be documented.
> We could more carefully reference count it, but I think that would
> actually be worse because the reference count would probably bounce
> through zero frequently.

I agree that this is awkward.  Is there a suggestion on how to do it
better?  We only want to initialize it if it's needed, and only
_mime_node_create knows that.  And we don't want to free it with
_mime_node_context_free, or something, only to have to reinitialize it
again with the next node or message.  Thoughts?

jamie.
part-000.sig (application/pgp-signature)

Thread: