fix use of uninit'd mem

This commit is contained in:
Emile Clark-Boman 2025-09-28 13:20:16 +10:00
parent b7c8c52212
commit dede047166
2 changed files with 18 additions and 9 deletions

View file

@ -1,5 +1,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "ct_alloc.h" #include "ct_alloc.h"
@ -16,29 +17,36 @@ end:
return fd; return fd;
} }
static inline void ct_resettermios(struct ct_term *restrict const term) {
term->termios = term->termios0;
}
int ct_applyterm(struct ct_term *restrict const term) {
return tcsetattr(term->fd, TCSANOW, &term->termios);
}
int ct_stashterm(struct ct_term **restrict term) { int ct_stashterm(struct ct_term **restrict term) {
struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term)); // struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term));
*term = (struct ct_term *)malloc(sizeof(struct ct_term));
memset(*term, 0, sizeof(struct ct_term));
// cast away the const to initialise these fields // cast away the const to initialise these fields
*(int *)&(*term)->fd = ct_tty((struct termios *)&(*term)->termios0); *(int *)&(*term)->fd = ct_tty((struct termios*)&(*term)->termios0);
if ((*term)->fd < 0) { if ((*term)->fd < 0) {
free(*term); free(*term);
return NERR; return NERR;
} }
// duplicate as termios0 will now be readonly // duplicate as termios0 will now be readonly
(*term)->termios = (*term)->termios0; ct_resettermios(*term);
ct_termsize(*term); return ct_termsize(*term);
return OK;
} }
void ct_resetterm(struct ct_term *const term) { void ct_resetterm(struct ct_term *const term) {
if (term->fd != -1) { if (term->fd != -1) {
ct_resettermios(term);
ct_applyterm(term);
close(term->fd); close(term->fd);
/* WARNING: should tcsetattr instead be called on term->fd before closing?
*/
tcsetattr(STDIN_FILENO, TCSANOW, &term->termios0);
} }
free(term); free(term);
} }

View file

@ -26,6 +26,7 @@ static inline int ct_termsize(struct ct_term *const term) {
} }
int ct_stashterm(struct ct_term **const term); int ct_stashterm(struct ct_term **const term);
int ct_applyterm(struct ct_term *const term);
void ct_resetterm(struct ct_term *const term); void ct_resetterm(struct ct_term *const term);
#endif /* _CURSETREE_TERM_H */ #endif /* _CURSETREE_TERM_H */