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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ct_alloc.h"
@ -16,8 +17,18 @@ end:
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) {
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
*(int *)&(*term)->fd = ct_tty((struct termios*)&(*term)->termios0);
@ -27,18 +38,15 @@ int ct_stashterm(struct ct_term **restrict term) {
return NERR;
}
// duplicate as termios0 will now be readonly
(*term)->termios = (*term)->termios0;
ct_termsize(*term);
return OK;
ct_resettermios(*term);
return ct_termsize(*term);
}
void ct_resetterm(struct ct_term *const term) {
if (term->fd != -1) {
ct_resettermios(term);
ct_applyterm(term);
close(term->fd);
/* WARNING: should tcsetattr instead be called on term->fd before closing?
*/
tcsetattr(STDIN_FILENO, TCSANOW, &term->termios0);
}
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_applyterm(struct ct_term *const term);
void ct_resetterm(struct ct_term *const term);
#endif /* _CURSETREE_TERM_H */