[PATCH] Fix window title when no dives selected

Sergey Starosek sergey.starosek at gmail.com
Sat May 18 06:15:35 PDT 2013


On Fri, May 17, 2013 at 6:41 PM, Dirk Hohndel <dirk at hohndel.org> wrote:

>
> I don't think this is correct. sizeof gives me len plus the trailing
> '\0' - so the actual space the string constant needs. With your change
> we have a buffer that is one character short...
>
> So does this fix an actual bug that you can see before the patch or is
> this just code that you saw and thought was wrong - because this would
> be wrong in the case of char buffer[] = "Subsurface: "; sizeof(buffer) /
> strlen(buffer);
>
> The advantage of doing it the way it's in the code before your patch is
> that the calculation is done at compile time (instead of runtime) and
> there is less risk of forgetting to allow for the trailing '\0'...
>

Agreed. Then patch may look like:

 @@ -108,12 +108,12 @@ const char *get_window_title(struct dive *dive)
                if (existing_filename) {
                        char *basename;
                        basename = g_path_get_basename(existing_filename);
-                       len1 = strlen("Subsurface: ");
+                       len1 = sizeof("Subsurface: ");
                        len2 = g_utf8_strlen(basename, -1);
                        sz = (len1 + len2) * sizeof(gunichar);
                        title = malloc(sz);
                        strncpy(title, "Subsurface: ", len1);
-                       g_utf8_strncpy(title + len1, basename, len2);
+                       g_utf8_strncpy(title + len1-1, basename, len2);
                } else {
                        title = strdup("Subsurface");
                }

Otherwise there's no filename in the window title.

Sergey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20130518/36e6d31a/attachment-0001.html>


More information about the subsurface mailing list