std::string str(c_str, strnlen(c_str, max_length));
At Christian Rau's request:
strnlen is specified in POSIX.1-2008 and available in GNU's glibc and the Microsoft run-time library. It is not yet found in some other systems; you may fall back to Gnulib's substitute.