Fix potential segfault in term_trim() functions