Branch: refs/heads/master Author: Colomban Wendling ban@herbesfolles.org Committer: Colomban Wendling ban@herbesfolles.org Date: Thu, 16 Nov 2023 09:47:12 UTC Commit: 7f630aaf917b733c76640574610017fc3a7c15ab https://github.com/geany/geany/commit/7f630aaf917b733c76640574610017fc3a7c15...
Log Message: ----------- Fix fairly nasty implicit integer conversion
Subtracting two unsigned values result in an unsigned value, and here to use a different sign on each side of a ternary operator.
src/tagmanager/tm_workspace.c: In function 'sort_found_tags': src/tagmanager/tm_workspace.c:909:45: warning: operand of '?:' changes signedness from 'int' to 'gulong' {aka 'long unsigned int'} due to unsignedness of other operand [-Wsign-compare] 909 | return info->sort_by_name ? g_strcmp0(t1->name, t2->name) : t2->line - t1->line; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is an especially valid concern as TMTag::line is an unsigned long, and the function returns an int, leading to possible sign swapping with extreme values. This is fairly theoretical as it's unlikely actual files have enough lines for this to actually matter, but still.
Modified Paths: -------------- src/tagmanager/tm_workspace.c
Modified: src/tagmanager/tm_workspace.c 7 lines changed, 6 insertions(+), 1 deletions(-) =================================================================== @@ -906,7 +906,12 @@ static gint sort_found_tags(gconstpointer a, gconstpointer b, gpointer user_data * followed by workspace tags, * followed by global tags */ if (t1->type & tm_tag_local_var_t && t2->type & tm_tag_local_var_t) - return info->sort_by_name ? g_strcmp0(t1->name, t2->name) : t2->line - t1->line; + { + if (info->sort_by_name) + return g_strcmp0(t1->name, t2->name); + else /* just like (t2->line - t1->line), but doesn't overflow converting to int */ + return (t2->line > t1->line) ? 1 : ((t2->line < t1->line) ? -1 : 0); + } else if (t1->type & tm_tag_local_var_t) return -1; else if (t2->type & tm_tag_local_var_t)
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).