Make string functions exception aware
This commit is contained in:
1 parent
2395ceb3d2
commit
5805038943
1 file changed
+13
-1
+13
-1
@@ -1070,6 +1070,10 @@ m_string_vprintf (m_string_t v, const char format[], va_list args)
|
||||
size_t alloc = m_string_capacity(v);
|
||||
size = vsnprintf (ptr, alloc, format, args);
|
||||
if (size > 0 && ((size_t) size+1 >= alloc) ) {
|
||||
// Reset the string in a clean state in case of exception
|
||||
// to keep the object in a clearable state
|
||||
M_IF_EXCEPTION( m_str1ng_set_size(v, 0) );
|
||||
M_IF_EXCEPTION( m_str1ng_get_cstr(v)[0] = 0 );
|
||||
// We have to realloc our string to fit the needed size
|
||||
ptr = m_str1ng_fit2size (v, (size_t) size + 1);
|
||||
alloc = m_string_capacity(v);
|
||||
@@ -1116,6 +1120,10 @@ m_string_cat_vprintf (m_string_t v, const char format[], va_list args)
|
||||
size_t alloc = m_string_capacity(v);
|
||||
size = vsnprintf (&ptr[old_size], alloc - old_size, format, args);
|
||||
if (size > 0 && (old_size+(size_t)size+1 >= alloc) ) {
|
||||
// Reset the string in a clean state in case of exception
|
||||
// to keep the object in a clearable state
|
||||
M_IF_EXCEPTION( m_str1ng_set_size(v, old_size) );
|
||||
M_IF_EXCEPTION( m_str1ng_get_cstr(v)[old_size] = 0 );
|
||||
// We have to realloc our string to fit the needed size
|
||||
ptr = m_str1ng_fit2size (v, old_size + (size_t) size + 1);
|
||||
alloc = m_string_capacity(v);
|
||||
@@ -1178,7 +1186,7 @@ m_string_fgets(m_string_t v, FILE *f, m_string_fgets_t arg)
|
||||
bool retcode = false; /* Nothing has been read yet */
|
||||
/* alloc - size is very unlikely to be bigger than INT_MAX
|
||||
but fgets accepts an int as the size argument */
|
||||
while (fgets(&ptr[size], (int) M_MIN( (alloc - size), (size_t) INT_MAX ), f) != NULL) {
|
||||
while (fgets(&ptr[size], (int) M_MIN( (alloc - 1 - size), (size_t) INT_MAX ), f) != NULL) {
|
||||
retcode = true; /* Something has been read */
|
||||
// fgets doesn't return the number of characters read, so we need to count.
|
||||
size += strlen(&ptr[size]);
|
||||
@@ -1192,6 +1200,10 @@ m_string_fgets(m_string_t v, FILE *f, m_string_fgets_t arg)
|
||||
M_STR1NG_CONTRACT(v);
|
||||
return retcode; /* Normal termination */
|
||||
} else if (ptr[size-1] != '\n' && !feof(f)) {
|
||||
// Reset the string in a clean state in case of exception
|
||||
// to keep the object in a clearable state
|
||||
M_IF_EXCEPTION( m_str1ng_set_size(v, size) );
|
||||
M_IF_EXCEPTION( m_str1ng_get_cstr(v)[size] = 0 );
|
||||
/* The string buffer is not big enough:
|
||||
increase it and continue reading */
|
||||
/* v cannot be stack alloc */
|
||||
|
||||
Reference in new issue
Block a user