Closed stefan11111 closed 1 month ago
Also, a patch which fixes some code that made the compiler throw warnings and which seemed suspicious:
+++ b/src/glib.c 2024-06-19 22:03:08.689574762 +0300
@@ -60,7 +60,8 @@
g_realloc( void *mem,
unsigned long size )
{
- return g_realloc( mem, size );
+ void *tmp = realloc ( mem, size );
+ return tmp ? tmp : mem;
}
GSList *
@@ -140,10 +141,10 @@
g_string_new( const char *str )
{
GString *string = malloc( sizeof(GString) );
- int len = strlen( str );
+ int len = strlen( str ) + 1;
string->str = malloc( len );
- string->len = len;
+ string->len = len - 1;
strcpy( string->str, str );
Nice find!
Nice find!
I saw the latest commit, and I still think the g_realloc implementation might be now what is desired.
On failure, realloc returns NULL and leaves the original chunk untouched. In this case, it might be preferable to return the original memory chunk, to not cause a memory leak. However, this depends on how g_realloc in used in the code. if it's checked for failure, it's not a problem. If it's simply supposed that g_realloc never fails, then it can be a problem.
From looking at the code, it seems that the latter is the case.
As a side note, glib is notoriously bad at handling memory allocation errors, and simply calls abort() if the allocation fails.
any ideas on the lto/strict aliasing thing?
I agree on realloc(), I note the point. For information, the internal libzvt/vt.c which calls g_realloc() comes directly from the old archive https://download.gnome.org/sources/libzvt/2.0/libzvt-2.0.1.tar.gz
I'm not used to enabling LTO (I should). No idea yet, but this strict aliasing issue is something to fix.
I made a pr to address the strict aliasing issue: https://github.com/directfb2/DFBTerm/pull/7 I will post the patch here too:
diff --git a/src/libzvt/vt.c b/src/libzvt/vt.c
index 1083968..0119689 100644
--- a/src/libzvt/vt.c
+++ b/src/libzvt/vt.c
@@ -1785,7 +1785,7 @@ vt_init(struct vt_em *vt, int width, int height)
vt->childpid = -1;
vt->keyfd = -1;
- vt->this_line = (struct vt_line *)vt->lines.head;
+ memcpy(&vt->this_line, &vt->lines.head, sizeof(struct vt_line*));
vt->scrollbacklines=0;
vt->scrollbackoffset=0;
The issue can also be fixed by adding more volatiles, but I like the memcpy approach more.
diff --git a/src/libzvt/vt.c b/src/libzvt/vt.c
index 1083968..008e448 100644
--- a/src/libzvt/vt.c
+++ b/src/libzvt/vt.c
@@ -1785,7 +1785,7 @@ vt_init(struct vt_em *vt, int width, int height)
vt->childpid = -1;
vt->keyfd = -1;
- vt->this_line = (struct vt_line *)vt->lines.head;
+ ((volatile struct vt_em *)vt)->this_line = (struct vt_line *)((volatile struct vt_em *)vt)->lines.head;
vt->scrollbacklines=0;
vt->scrollbackoffset=0;
I also made a patch to fix the -Wunused-result warnings. Should I add it to the pr?
diff --git a/src/libzvt/gnome-pty-helper.c b/src/libzvt/gnome-pty-helper.c
index 9601a68..3df433b 100644
--- a/src/libzvt/gnome-pty-helper.c
+++ b/src/libzvt/gnome-pty-helper.c
@@ -489,11 +489,11 @@ open_ptys (int utmp, int wtmp, int lastlog)
/* drop privileges to the user level */
#if defined(HAVE_SETEUID)
- seteuid (pwent->pw_uid);
- setegid (pwent->pw_gid);
+ (void)!seteuid (pwent->pw_uid);
+ (void)!setegid (pwent->pw_gid);
#elif defined(HAVE_SETREUID)
- setreuid (savedUid, pwent->pw_uid);
- setregid (savedGid, pwent->pw_gid);
+ (void)!setreuid (savedUid, pwent->pw_uid);
+ (void)!setregid (savedGid, pwent->pw_gid);
#else
#error "No means to drop privileges! Huge security risk! Won't compile."
#endif
@@ -502,25 +502,25 @@ open_ptys (int utmp, int wtmp, int lastlog)
/* Restore saved priveleges to root */
#ifdef HAVE_SETEUID
- seteuid (savedUid);
- setegid (savedGid);
+ (void)!seteuid (savedUid);
+ (void)!setegid (savedGid);
#elif defined(HAVE_SETREUID)
- setreuid (pwent->pw_uid, savedUid);
- setregid (pwent->pw_gid, savedGid);
+ (void)!setreuid (pwent->pw_uid, savedUid);
+ (void)!setregid (pwent->pw_gid, savedGid);
#else
#error "No means to raise privileges! Huge security risk! Won't compile."
#endif
/* openpty() failed, reject request */
if (status == -1){
result = 0;
- write (STDIN_FILENO, &result, sizeof (result));
+ (void)!write (STDIN_FILENO, &result, sizeof (result));
return 0;
}
/* a bit tricky, we re-do the part of the openpty() */
/* that required root priveleges, and, hence, failed */
group_info = getgrnam ("tty");
- fchown (slave_pty, getuid (), group_info ? group_info->gr_gid : -1);
+ (void)!fchown (slave_pty, getuid (), group_info ? group_info->gr_gid : -1);
fchmod (slave_pty, S_IRUSR | S_IWUSR | S_IWGRP);
/* It's too late to call revoke at this time... */
/* revoke(term_name); */
diff --git a/src/libzvt/subshell.c b/src/libzvt/subshell.c
index 0378e98..db84669 100644
--- a/src/libzvt/subshell.c
+++ b/src/libzvt/subshell.c
@@ -75,7 +75,7 @@ sigchld_handler (int signo)
if (waitpid (child->pid, &status, WNOHANG) == child->pid){
child->exit_status = status;
child->dead = 1;
- write (child->fd, "D", 1);
+ (void)!write (child->fd, "D", 1);
return;
}
}
@@ -373,7 +373,7 @@ zvt_init_subshell (struct vt_em *vt, char *pty_name, int log)
} else {
close (slave_pty);
- pipe(p);
+ (void)!pipe(p);
vt->msgfd = p [0];
@@ -391,7 +391,7 @@ zvt_init_subshell (struct vt_em *vt, char *pty_name, int log)
pid = waitpid (vt->childpid, &status, WUNTRACED | WNOHANG);
if (pid == vt->childpid && child->pid >= 0){
child->pid = 0;
- write (child->fd, "D", 1);
+ (void)!write (child->fd, "D", 1);
return -1;
}
@@ -435,8 +435,8 @@ zvt_shutdown_subshell (struct vt_em *vt)
/* shutdown pty through helper */
if (vt->pty_tag) {
op = GNOME_PTY_CLOSE_PTY;
- write (helper_socket_protocol [0], &op, sizeof (op));
- write (helper_socket_protocol [0], &vt->pty_tag, sizeof (vt->pty_tag));
+ (void)!write (helper_socket_protocol [0], &op, sizeof (op));
+ (void)!write (helper_socket_protocol [0], &vt->pty_tag, sizeof (vt->pty_tag));
vt->pty_tag = NULL;
}
Good to see you fixed this runtime failure with -flto
Your patch regarding warnings in libzvt looks good, feel free to open a PR about it.
Good to see you fixed this runtime failure with -flto
Your patch regarding warnings in libzvt looks good, feel free to open a PR about it.
I opened a pr. also put a question in the message. https://github.com/directfb2/DFBTerm/pull/8
when building dfbterm with -flto, dfbterm segfaults when ran. this is fixed when also passing -fno-strict-aliasing to gcc, which leads me to think this is caused by some ub. I think this may be from one of the functions with void* args which are casted to other types, and the gdb backtraces seem to indicate the same.
from gdb: