gmio_stl: fix segfault in gmio_stla_infos_probe()

GitHub issue #8
This commit is contained in:
Hugues Delorme 2017-03-21 18:43:47 +01:00
parent f62519109d
commit f54dd0a1fa
4 changed files with 36 additions and 17 deletions

View File

@ -96,6 +96,8 @@ int gmio_stla_infos_probe(
if (flags == 0)
return err;
if (flag_stla_solidname && infos->stla_solidname == NULL)
return GMIO_STL_ERROR_INFO_NULL_SOLIDNAME;
if (!gmio_check_memblock(&err, &opts->stream_memblock))
return err;
@ -127,7 +129,6 @@ int gmio_stla_infos_probe(
gmio_stla_parse_solidname_beg(&parse_data);
/* Copy parsed solid name into infos->stla_solid_name */
{
const struct gmio_string* strbuff = &parse_data.token_str;
const size_t name_len_for_cpy =
GMIO_MIN(infos->stla_solidname_maxlen - 1, strbuff->len);
@ -135,10 +136,10 @@ int gmio_stla_infos_probe(
strncpy(infos->stla_solidname, strbuff->ptr, name_len_for_cpy);
/* Null terminate C string */
if (name_len_for_cpy != 0)
infos->stla_solidname[name_len_for_cpy] = 0;
infos->stla_solidname[name_len_for_cpy] = '\0';
else if (infos->stla_solidname_maxlen != 0)
infos->stla_solidname[0] = 0;
}
infos->stla_solidname[0] = '\0';
sstream = parse_data.strstream;
}

View File

@ -48,27 +48,33 @@ enum gmio_stl_error
/*! Common STL write error indicating gmio_stl_mesh::func_get_triangle()
* pointer is NULL */
GMIO_STL_ERROR_NULL_FUNC_GET_TRIANGLE = GMIO_STL_ERROR_TAG + 0x02,
GMIO_STL_ERROR_NULL_FUNC_GET_TRIANGLE,
/* Specific error codes returned by STL_ascii read function */
/*! Parsing error occured due to malformed STL ascii input */
GMIO_STL_ERROR_PARSING = GMIO_STL_ERROR_TAG + 0x100,
GMIO_STL_ERROR_PARSING,
/*! Invalid max number of decimal significants digits must be in [1..9] */
GMIO_STL_ERROR_INVALID_FLOAT32_PREC = GMIO_STL_ERROR_TAG + 0x101,
GMIO_STL_ERROR_INVALID_FLOAT32_PREC,
/* Specific error codes returned by STL_binary read/write functions */
/*! The byte order argument supplied is not supported, must be little or
* big endian */
GMIO_STL_ERROR_UNSUPPORTED_BYTE_ORDER = GMIO_STL_ERROR_TAG + 0x1000,
GMIO_STL_ERROR_UNSUPPORTED_BYTE_ORDER,
/*! Error occured when reading header data in gmio_stlb_read() */
GMIO_STL_ERROR_HEADER_WRONG_SIZE = GMIO_STL_ERROR_TAG + 0x1001,
GMIO_STL_ERROR_HEADER_WRONG_SIZE,
/*! Error occured when reading facet count in gmio_stlb_read() */
GMIO_STL_ERROR_FACET_COUNT = GMIO_STL_ERROR_TAG + 0x1002
GMIO_STL_ERROR_FACET_COUNT,
/* Specific error codes returned by STL infos probe functions */
/*! Flag \c GMIO_STLA_INFO_FLAG_SOLIDNAME is on but supplied
* gmio_stl_infos::stla_solidname string is NULL */
GMIO_STL_ERROR_INFO_NULL_SOLIDNAME
};
/*! @} */

View File

@ -61,6 +61,7 @@ const char* all_tests()
UTEST_RUN(test_stl_internal__error_check);
UTEST_RUN(test_stl_infos);
UTEST_RUN(test_stl_infos_github8);
UTEST_RUN(test_stl_read);
UTEST_RUN(test_stl_read_multi_solid);

View File

@ -101,3 +101,14 @@ static const char* test_stl_infos()
}
return NULL;
}
static const char* test_stl_infos_github8()
{
const char* filepath = "models/solid_empty.stla";
struct gmio_stl_infos infos = {0};
infos.stla_solidname_maxlen = 512;
const int error = gmio_stl_infos_probe_file(
&infos, filepath, GMIO_STL_INFO_FLAG_ALL, NULL);
UTEST_COMPARE_INT(error, GMIO_STL_ERROR_INFO_NULL_SOLIDNAME);
return NULL;
}