[PATCH] dive.c:merge_text(): Slighly better string merge logic

Dirk Hohndel dirk at hohndel.org
Fri Mar 8 10:06:20 PST 2013


"Lubomir I. Ivanov" <neolit123 at gmail.com> writes:

> On 8 March 2013 19:46, Dirk Hohndel <dirk at hohndel.org> wrote:
>> "Lubomir I. Ivanov" <neolit123 at gmail.com> writes:
>>
>>> On 8 March 2013 18:55, Dirk Hohndel <dirk at hohndel.org> wrote:
>>>> I don't agree with this patch.
>>>>
>>>> Imagine that a="" and b="Great dive location"
>>>> The existing code returns a copy of "Great dive location" while your
>>>> code returns "() or (Great dive locations)".
>>>> That is not an improvement.
>>>>
>>>
>>> yeah, it has to be improved further.
>>>
>>>> Can you explain in more detail what you are trying to fix?
>>>>
>>>
>>> the backtrace of #90, showed:
>>> (a=0x348d770 "", b=0x0) at dive.c:911
>>>
>>> where 'a' is "", but 'b' is NULL, so it passes NULL to strdup().
>>
>> So test for that :-)
>>
>> I can fix this myself if you prefer.
>>
>
> i'll have to go AFK in a bit, so if you think this should be fixed
> right now, please go ahead.
> not sure on a wider perspective, if 'a' is "" and 'b' is NULL, that we
> could just return NULL, instead of doing a strdup("").

That's what I ended up doing:

diff --git a/dive.c b/dive.c
index 1b4c783..034fb15 100644
--- a/dive.c
+++ b/dive.c
@@ -908,11 +908,11 @@ static char *merge_text(const char *a, const char *b)
        if (!a && !b)
                return NULL;
        if (!a || !*a)
-               return strdup(b);
+               return b ? strdup(b) : NULL
        if (!b || !*b)
                return strdup(a);
        if (!strcmp(a,b))
-               return strdup(a);
+               return a ? strdup(a) : NULL;
        res = malloc(strlen(a) + strlen(b) + 32);
        if (!res)
                return (char *)a;


/D


More information about the subsurface mailing list