From 045d6ae0b042d4e6e3c4540c32559c58cb4153b0 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 28 May 2013 11:12:18 -0400 Subject: [PATCH] KWSys: Fix SystemTools::FileIsDirectory with long paths (#14176) Allocate a buffer large enough to hold the input path when removing a trailing slash. Use a local stack buffer when it is large enough and fall back to heap allocation otherwise. --- Source/kwsys/SystemTools.cxx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 22bf193b0..8b25d608d 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -2742,14 +2742,23 @@ bool SystemTools::FileIsDirectory(const char* name) } // Remove any trailing slash from the name. - char buffer[KWSYS_SYSTEMTOOLS_MAXPATH]; + char local_buffer[KWSYS_SYSTEMTOOLS_MAXPATH]; + std::string string_buffer; size_t last = length-1; if(last > 0 && (name[last] == '/' || name[last] == '\\') && strcmp(name, "/") !=0) { - memcpy(buffer, name, last); - buffer[last] = 0; - name = buffer; + if(last < sizeof(local_buffer)) + { + memcpy(local_buffer, name, last); + local_buffer[last] = 0; + name = local_buffer; + } + else + { + string_buffer.append(name, last); + name = string_buffer.c_str(); + } } // Now check the file node type.