installUnicodeWidthTable: on range population failure, deallocate the custom table

This commit is contained in:
Jonathan Daugherty 2020-03-03 16:17:02 -08:00
parent 7845acd565
commit e07f287d07
2 changed files with 20 additions and 1 deletions

View File

@ -316,3 +316,18 @@ int vty_custom_table_ready()
{
return ((custom_table != NULL) && custom_table_ready);
}
// Deallocate the custom width table.
//
// This does nothing if there is no allocated custom width table, or if
// there is one but it is in use (marked ready). This is only useful if
// an initial allocation succeeds, but range population fails, after
// which point the application may want to deallocate the table to avoid
// leaving it in an intermediate state.
void vty_deallocate_custom_table()
{
if ((custom_table != NULL) && (!custom_table_ready)) {
free(custom_table);
custom_table = NULL;
}
}

View File

@ -27,6 +27,9 @@ foreign import ccall unsafe "vty_activate_custom_table"
foreign import ccall unsafe "vty_custom_table_ready"
c_isCustomTableReady :: IO Int
foreign import ccall unsafe "vty_deallocate_custom_table"
deallocateCustomTable :: IO ()
-- | Returns True if and only if a custom table has been allocated and
-- marked as ready for use. Returns False otherwise.
isCustomTableReady :: IO Bool
@ -80,7 +83,8 @@ installUnicodeWidthTable table = do
(rangeSize r)
(rangeColumns r)
when (result /= 0) $
when (result /= 0) $ do
deallocateCustomTable
error $ "installUnicodeWidthTable: error installing range " <>
show r <> ", status " <> show result