Skip to content

Commit c281930

Browse files
committed
main: fix a potential bug in the code making filename sans extension
baseFilenameSansExtensionNew("a.in.in", ".in.in") could not return "a" with the original code. Signed-off-by: Masatake YAMATO <[email protected]>
1 parent bf0bbc4 commit c281930

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

extra-cmds/utiltest.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "intern.h"
1414
#include "numarray.h"
1515
#include "routines.h"
16+
#include "routines_p.h"
1617
#include "vstring.h"
1718
#include <string.h>
1819

@@ -277,6 +278,31 @@ static void test_routines_strrstr(void)
277278
TEST_CHECK(strcmp(strrstr("abcdcdb", "cd"), "cdb") == 0);
278279
}
279280

281+
static void test_routines_baseFilenameSansExtensionNew(void)
282+
{
283+
char *bs;
284+
285+
TEST_CHECK ((bs = baseFilenameSansExtensionNew ("a.in", ".in"))
286+
&& strcmp(bs, "a") == 0);
287+
if (bs)
288+
eFree (bs);
289+
290+
TEST_CHECK ((bs = baseFilenameSansExtensionNew ("x/b.in", ".in"))
291+
&& strcmp(bs, "b") == 0);
292+
if (bs)
293+
eFree (bs);
294+
295+
TEST_CHECK ((bs = baseFilenameSansExtensionNew ("c.in.in", ".in.in"))
296+
&& strcmp(bs, "c") == 0);
297+
if (bs)
298+
eFree (bs);
299+
300+
TEST_CHECK ((bs = baseFilenameSansExtensionNew ("/y/d.in.in", ".in.in"))
301+
&& strcmp(bs, "d") == 0);
302+
if (bs)
303+
eFree (bs);
304+
}
305+
280306
static void test_vstring_ncats(void)
281307
{
282308
vString *vstr = vStringNew ();
@@ -351,6 +377,7 @@ TEST_LIST = {
351377
{ "intern", test_intern },
352378
{ "numarray", test_numarray },
353379
{ "routines/strrstr", test_routines_strrstr },
380+
{ "routines/baseFilenameSansExtensionNew", test_routines_baseFilenameSansExtensionNew },
354381
{ "vstring/ncats", test_vstring_ncats },
355382
{ "vstring/truncate_leading", test_vstring_truncate_leading },
356383
{ "vstring/EqC", test_vstring_eqc },

main/routines.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ extern char* baseFilenameSansExtensionNew (const char *const fileName,
664664
const char *const base = baseFilename (fileName);
665665
char* shorten_base;
666666

667-
pDelimiter = strrchr (base, templateExt[0]);
667+
pDelimiter = strrstr (base, templateExt);
668668

669669
if (pDelimiter && (strcmp (pDelimiter, templateExt) == 0))
670670
{

0 commit comments

Comments
 (0)