[cmake-developers] [CMake 0015735]: Non-ascii characters in POST_BUILD commands truncated by CMake (if Ninja generator used) on Linux

Mantis Bug Tracker mantis at public.kitware.com
Thu Sep 10 06:50:17 EDT 2015


The following issue has been SUBMITTED. 
====================================================================== 
http://public.kitware.com/Bug/view.php?id=15735 
====================================================================== 
Reported By:                Pavel Solodovnikov
Assigned To:                
====================================================================== 
Project:                    CMake
Issue ID:                   15735
Category:                   CMake
Reproducibility:            always
Severity:                   major
Priority:                   high
Status:                     new
====================================================================== 
Date Submitted:             2015-09-10 06:50 EDT
Last Modified:              2015-09-10 06:50 EDT
====================================================================== 
Summary:                    Non-ascii characters in POST_BUILD commands
truncated by CMake (if Ninja generator used) on Linux
Description: 
CMake doesn't handle backslash escaped strings properly in POST_BUILD commands
added via "add_custom_command" if provided commands end with non-ascii
characters.

Here is a simple example that represents the issue: I have a trivial
CMakeLists.txt with one library target:

cmake_minimum_required(VERSION 2.8.11)

project(example)

add_library(ex abc.cpp)

add_custom_command(TARGET ex POST_BUILD COMMAND cmd1 "Пример с
пробелами")

For example, I want to execute post build command "cmd1" and pass a parameter
"Пример с пробелами" that contains russian characters.

If Ninja generator is specified, CMake generates ill-formed "build.ninja" file.
The point of interest in produced build.ninja file looks like that:

build libex.a: CXX_STATIC_LIBRARY_LINKER CMakeFiles/ex.dir/abc.cpp.o
  POST_BUILD = cd /mnt/sources/work/example && cmd1 Пример\ с\
  PRE_LINK = :
  TARGET_PDB = ex.a.dbg

The last word in passed parameter "Пример с пробелами" had been
truncated by CMake up to the first encountered space so I can't execute my post
build commands properly, since the argument string is corrupted now.

After debugging CMake a little, I've discovered that the source of this error is
in function "cmSystemTools::TrimWhitespace" (cmSystemTools.cxx), which doesn't
take into account the fact that std::string individual characters are of 'char'
type which is (in most circumstances) signed by default.

Hence there are errors in conditions:

  while(start != s.end() && *start <= ' ')
    ++start;

and 

  while(*stop <= ' ')
    --stop;

If *stop doesn't fit into 0-127 range(ascii table) as in my example with russian
letters, *stop appears to be less than 0, so condition to trim characters is
true and it silently chops the last word out of my string until it encounters a
space char.

In our projects we use such post-build commands very widely (for example to copy
produced binaries to a specific directory which have a cyrillic name) so this
issue is somewhat crucial for us.

I've attached a patch (patch.diff inside uploaded archive) that resolves this
issue. It simply adds conversion of *start and *stop explicitly to unsigned
char.

Steps to Reproduce: 
1) Extract attached example.tar
2) Run "cmake -G "Ninja" ." inside extracted dir.
3) Examine produced build.ninja file.

Additional Information: 
Affected platform is Linux, Windows uses double quote escaping and the issue
does not arise.

This bug persists in CMake releases from 2.8.11 up to the latest 3.3.1 (at least
this is what I've observed while looking into sources).
====================================================================== 

Issue History 
Date Modified    Username       Field                    Change               
====================================================================== 
2015-09-10 06:50 Pavel SolodovnikovNew Issue                                    
2015-09-10 06:50 Pavel SolodovnikovFile Added: example.zip                      
======================================================================



More information about the cmake-developers mailing list