Index: thunar-vfs/thunar-vfs-path.c =================================================================== --- thunar-vfs/thunar-vfs-path.c (revision 23810) +++ thunar-vfs/thunar-vfs-path.c (working copy) @@ -1058,21 +1058,28 @@ { for (;;) { - /* increase the buffer automatically if required (already including the line break) */ - n = thunar_vfs_path_to_uri (lp->data, buffer + bufpos, bufsize - (bufpos + 2), NULL); - if (G_UNLIKELY (n < 0)) + /* determine the size required to store the URI and the line break */ + n = thunar_vfs_path_escape_uri_length (lp->data) + 2; + if (n > (bufsize - bufpos)) { + /* automatically increase the buffer */ bufsize += 512; buffer = g_realloc (buffer, bufsize + 1); continue; } + /* append the URI to the buffer */ + n = thunar_vfs_path_escape_uri (lp->data, buffer + bufpos); + /* shift the buffer position */ bufpos += (n - 1); /* append a line break */ buffer[bufpos++] = '\r'; buffer[bufpos++] = '\n'; + + /* sanity checks */ + _thunar_vfs_assert (bufpos <= bufsize); break; } }