Skip to content

Commit eee1b87

Browse files
Copilotprobonopd
andcommitted
Fix all compiler warnings from strict flags (-Wall -Wextra -Wconversion -Werror)
- Add (void) casts for unused function parameters - Fix sign conversion warnings with explicit casts - Change variable types to match library function signatures - Fix format string specifiers (%d -> %zu, %lu -> %lld) - Add __attribute__((unused)) to static functions not currently used - Replace sprintf with snprintf for buffer safety - Fix strncpy to ensure null termination - Add version check for deprecated g_spawn_check_exit_status - Add missing return statement in archToName() - Fix sign comparison warnings by adding explicit casts All changes maintain correctness while satisfying strict compiler warnings. Co-authored-by: probonopd <[email protected]>
1 parent 10b9ade commit eee1b87

File tree

4 files changed

+57
-46
lines changed

4 files changed

+57
-46
lines changed

src/appimagetool.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ int sfs_mksquashfs(char *source, char *destination, int offset) {
123123

124124
guint sqfs_opts_len = sqfs_opts ? g_strv_length(sqfs_opts) : 0;
125125

126-
int max_num_args = sqfs_opts_len + 22;
126+
int max_num_args = (int)sqfs_opts_len + 22;
127127
char* args[max_num_args];
128128

129129
int i = 0;
@@ -270,9 +270,9 @@ static void replacestr(char *line, const char *search, const char *replace)
270270
if ((sp = strstr(line, search)) == NULL) {
271271
return;
272272
}
273-
int search_len = strlen(search);
274-
int replace_len = strlen(replace);
275-
int tail_len = strlen(sp+search_len);
273+
size_t search_len = strlen(search);
274+
size_t replace_len = strlen(replace);
275+
size_t tail_len = strlen(sp+search_len);
276276

277277
memmove(sp+replace_len,sp+search_len,tail_len+1);
278278
memcpy(sp, replace, replace_len);
@@ -304,6 +304,8 @@ gchar* archToName(fARCH arch) {
304304
case fARCH_x86_64:
305305
return "x86_64";
306306
}
307+
// Should never reach here if all enum values are handled
308+
return "unknown";
307309
}
308310

309311
gchar* getArchName(bool* archs) {
@@ -464,10 +466,10 @@ bool readFile(char* filename, size_t* size, char** buffer) {
464466
long fsize = ftell(f);
465467
fseek(f, 0, SEEK_SET);
466468

467-
char *indata = malloc(fsize);
468-
fread(indata, fsize, 1, f);
469+
char *indata = malloc((size_t)fsize);
470+
fread(indata, (size_t)fsize, 1, f);
469471
fclose(f);
470-
*size = (int)fsize;
472+
*size = (size_t)fsize;
471473
*buffer = indata;
472474
return TRUE;
473475
}
@@ -694,7 +696,11 @@ main (int argc, char *argv[])
694696
g_spawn_command_line_sync(command_line, &out, NULL, &exit_status, &error);
695697

696698
// g_spawn_command_line_sync might have set error already, in that case we don't want to overwrite
699+
#if GLIB_CHECK_VERSION(2, 70, 0)
700+
if (error != NULL || !g_spawn_check_wait_status(exit_status, &error)) {
701+
#else
697702
if (error != NULL || !g_spawn_check_exit_status(exit_status, &error)) {
703+
#endif
698704
if (error == NULL) {
699705
g_printerr("Failed to run 'git rev-parse --short HEAD, but failed to interpret GLib error state: %d\n", exit_status);
700706
} else {
@@ -776,7 +782,8 @@ main (int argc, char *argv[])
776782
const char* const env_app_name = getenv("APPIMAGETOOL_APP_NAME");
777783
if (env_app_name != NULL) {
778784
fprintf(stderr, "Using user-specified app name: %s\n", env_app_name);
779-
strncpy(app_name_for_filename, env_app_name, PATH_MAX);
785+
strncpy(app_name_for_filename, env_app_name, PATH_MAX - 1);
786+
app_name_for_filename[PATH_MAX - 1] = '\0';
780787
} else {
781788
const gchar* const desktop_file_app_name = get_desktop_entry(kf, "Name");
782789
sprintf(app_name_for_filename, "%s", desktop_file_app_name);
@@ -797,9 +804,9 @@ main (int argc, char *argv[])
797804

798805
// if $VERSION is specified, we embed it into the filename
799806
if (version_env != NULL) {
800-
sprintf(dest_path, "%s-%s-%s.AppImage", app_name_for_filename, version_env, arch);
807+
snprintf(dest_path, sizeof(dest_path), "%s-%s-%s.AppImage", app_name_for_filename, version_env, arch);
801808
} else {
802-
sprintf(dest_path, "%s-%s.AppImage", app_name_for_filename, arch);
809+
snprintf(dest_path, sizeof(dest_path), "%s-%s.AppImage", app_name_for_filename, arch);
803810
}
804811

805812
destination = strdup(dest_path);
@@ -922,9 +929,9 @@ main (int argc, char *argv[])
922929
}
923930
}
924931
if (verbose)
925-
printf("Size of the embedded runtime: %d bytes\n", size);
926-
927-
int result = sfs_mksquashfs(source, destination, size);
932+
printf("Size of the embedded runtime: %zu bytes\n", size);
933+
934+
int result = sfs_mksquashfs(source, destination, (int)size);
928935
if(result != 0)
929936
die("sfs_mksquashfs error");
930937

@@ -961,7 +968,7 @@ main (int argc, char *argv[])
961968
int ret = snprintf(buf, sizeof(buf), "gh-releases-zsync|%s|%s|latest|%s*-%s.AppImage.zsync", github_repository_owner, github_repository_name, app_name_for_filename, arch);
962969
if (ret < 0) {
963970
die("snprintf error");
964-
} else if (ret >= sizeof(buf)) {
971+
} else if ((size_t)ret >= sizeof(buf)) {
965972
die("snprintf buffer overflow");
966973
}
967974
updateinformation = buf;
@@ -996,7 +1003,7 @@ main (int argc, char *argv[])
9961003
int ret = snprintf(buf, sizeof(buf), "gh-releases-zsync|%s|%s|%s|%s*-%s.AppImage.zsync", parts[0], parts[1], channel, app_name_for_filename, arch);
9971004
if (ret < 0) {
9981005
die("snprintf error");
999-
} else if (ret >= sizeof(buf)) {
1006+
} else if ((size_t)ret >= sizeof(buf)) {
10001007
die("snprintf buffer overflow");
10011008
}
10021009
updateinformation = buf;
@@ -1014,7 +1021,7 @@ main (int argc, char *argv[])
10141021
int ret = snprintf(buf, sizeof(buf), "zsync|%s/-/jobs/artifacts/%s/raw/%s-%s.AppImage.zsync?job=%s", CI_PROJECT_URL, CI_COMMIT_REF_NAME, app_name_for_filename, arch, CI_JOB_NAME);
10151022
if (ret < 0) {
10161023
die("snprintf error");
1017-
} else if (ret >= sizeof(buf)) {
1024+
} else if ((size_t)ret >= sizeof(buf)) {
10181025
die("snprintf buffer overflow");
10191026
}
10201027
updateinformation = buf;
@@ -1061,7 +1068,7 @@ main (int argc, char *argv[])
10611068
FILE *fpdst2 = fopen(destination, "r+");
10621069
if (fpdst2 == NULL)
10631070
die("Not able to open the destination file for writing, aborting");
1064-
fseek(fpdst2, ui_offset, SEEK_SET);
1071+
fseek(fpdst2, (long)ui_offset, SEEK_SET);
10651072
// fseek(fpdst2, ui_offset, SEEK_SET);
10661073
// fwrite(0x00, 1, 1024, fpdst); // FIXME: Segfaults; why?
10671074
// fseek(fpdst, ui_offset, SEEK_SET);
@@ -1107,7 +1114,7 @@ main (int argc, char *argv[])
11071114
die("Failed to open AppImage for updating");
11081115
}
11091116

1110-
if (fseek(destinationfp, digest_md5_offset, SEEK_SET) != 0) {
1117+
if (fseek(destinationfp, (long)digest_md5_offset, SEEK_SET) != 0) {
11111118
fclose(destinationfp);
11121119
die("Failed to embed MD5 digest: could not seek to section offset");
11131120
}
@@ -1141,7 +1148,7 @@ main (int argc, char *argv[])
11411148

11421149
if (verbose) {
11431150
fprintf(stderr, "Running zsyncmake process: ");
1144-
for (gint j = 0; j < (sizeof(zsyncmake_command) / sizeof(char*) - 1); ++j) {
1151+
for (gint j = 0; j < (gint)(sizeof(zsyncmake_command) / sizeof(char*) - 1); ++j) {
11451152
fprintf(stderr, "'%s' ", zsyncmake_command[j]);
11461153
}
11471154
fprintf(stderr, "\n");

src/appimagetool_fetch_runtime.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ bool fetch_runtime(char *arch, size_t *size, char **buffer, bool verbose) {
281281

282282
auto runtimeData = response.data();
283283

284-
if (runtimeData.size() != response.contentLength()) {
284+
if (runtimeData.size() != static_cast<std::vector<char>::size_type>(response.contentLength())) {
285285
std::cerr << "Error: downloaded data size of " << runtimeData.size()
286286
<< " does not match content-length of " << response.contentLength() << std::endl;
287287
return false;
288288
}
289289

290-
*size = response.contentLength();
290+
*size = static_cast<size_t>(response.contentLength());
291291

292-
*buffer = (char *) calloc(response.contentLength() + 1, 1);
292+
*buffer = (char *) calloc(static_cast<size_t>(response.contentLength()) + 1, 1);
293293

294294
if (*buffer == nullptr) {
295295
std::cerr << "Failed to allocate buffer" << std::endl;

src/appimagetool_sign.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ char* get_passphrase_from_environment() {
2020
}
2121

2222
gpgme_error_t gpgme_passphrase_callback(void* hook, const char* uid_hint, const char* passphrase_info, int prev_was_valid, int fd) {
23+
(void) hook;
2324
(void) passphrase_info;
2425
(void) prev_was_valid;
2526

@@ -45,6 +46,7 @@ gpgme_error_t gpgme_passphrase_callback(void* hook, const char* uid_hint, const
4546
}
4647

4748
gpgme_error_t gpgme_status_callback(void* hook, const char* keyword, const char* args) {
49+
(void) hook;
4850
assert(hook == gpgme_hook);
4951

5052
fprintf(stderr, "[gpgme] %s: %s\n", keyword, args);
@@ -221,7 +223,7 @@ bool embed_data_in_elf_section(const char* filename, const char* elf_section, gp
221223
return false;
222224
}
223225

224-
if (data_size > key_section_length) {
226+
if ((unsigned long)data_size > key_section_length) {
225227
fprintf(stderr, "[sign] cannot embed key in AppImage: size exceeds reserved ELF section size\n");
226228
gpg_release_resources();
227229
return false;
@@ -231,10 +233,10 @@ bool embed_data_in_elf_section(const char* filename, const char* elf_section, gp
231233
char data_buffer[data_size];
232234

233235
size_t total_bytes_read = 0;
234-
size_t bytes_read = 0;
236+
ssize_t bytes_read = 0;
235237

236238
for (;;) {
237-
bytes_read = gpgme_data_read(data, data_buffer, data_size);
239+
bytes_read = gpgme_data_read(data, data_buffer + total_bytes_read, (size_t)(data_size - (off_t)total_bytes_read));
238240

239241
// EOF
240242
if (bytes_read == 0) {
@@ -248,15 +250,15 @@ bool embed_data_in_elf_section(const char* filename, const char* elf_section, gp
248250
return false;
249251
}
250252

251-
total_bytes_read += bytes_read;
253+
total_bytes_read += (size_t)bytes_read;
252254
}
253255

254-
if (total_bytes_read != data_size) {
256+
if ((off_t)total_bytes_read != data_size) {
255257
fprintf(
256258
stderr,
257-
"[sign] failed to read entire data from data object (%lu != %lu)\n",
259+
"[sign] failed to read entire data from data object (%zu != %lld)\n",
258260
total_bytes_read,
259-
data_size
261+
(long long)data_size
260262
);
261263
gpg_release_resources();
262264
return false;
@@ -278,7 +280,7 @@ bool embed_data_in_elf_section(const char* filename, const char* elf_section, gp
278280
}
279281

280282
// write at once
281-
if (fwrite(data_buffer, sizeof(char), data_size, destinationfp) != data_size) {
283+
if (fwrite(data_buffer, sizeof(char), (size_t)data_size, destinationfp) != (size_t)data_size) {
282284
fprintf(stderr, "[sign] failed to write signature to AppImage file\n");
283285
gpg_release_resources();
284286
fclose(destinationfp);

src/elf.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ static uint64_t file64_to_cpu(uint64_t val)
4747
return val;
4848
}
4949

50+
__attribute__((unused))
5051
static off_t read_elf32(FILE* fd)
5152
{
5253
Elf32_Ehdr ehdr32;
5354
Elf32_Shdr shdr32;
5455
off_t last_shdr_offset;
55-
ssize_t ret;
56+
size_t ret;
5657
off_t sht_end, last_section_end;
5758

5859
fseeko(fd, 0, SEEK_SET);
5960
ret = fread(&ehdr32, 1, sizeof(ehdr32), fd);
60-
if (ret < 0 || (size_t)ret != sizeof(ehdr32)) {
61+
if (ret != sizeof(ehdr32)) {
6162
fprintf(stderr, "Read of ELF header from %s failed: %s\n",
6263
fname, strerror(errno));
6364
return -1;
@@ -67,32 +68,33 @@ static off_t read_elf32(FILE* fd)
6768
ehdr.e_shentsize = file16_to_cpu(ehdr32.e_shentsize);
6869
ehdr.e_shnum = file16_to_cpu(ehdr32.e_shnum);
6970

70-
last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1));
71+
last_shdr_offset = (off_t)(ehdr.e_shoff + ((uint64_t)ehdr.e_shentsize * (ehdr.e_shnum - 1)));
7172
fseeko(fd, last_shdr_offset, SEEK_SET);
7273
ret = fread(&shdr32, 1, sizeof(shdr32), fd);
73-
if (ret < 0 || (size_t)ret != sizeof(shdr32)) {
74+
if (ret != sizeof(shdr32)) {
7475
fprintf(stderr, "Read of ELF section header from %s failed: %s\n",
7576
fname, strerror(errno));
7677
return -1;
7778
}
7879

7980
/* ELF ends either with the table of section headers (SHT) or with a section. */
80-
sht_end = ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum);
81-
last_section_end = file64_to_cpu(shdr32.sh_offset) + file64_to_cpu(shdr32.sh_size);
81+
sht_end = (off_t)(ehdr.e_shoff + ((uint64_t)ehdr.e_shentsize * ehdr.e_shnum));
82+
last_section_end = (off_t)(file64_to_cpu(shdr32.sh_offset) + file64_to_cpu(shdr32.sh_size));
8283
return sht_end > last_section_end ? sht_end : last_section_end;
8384
}
8485

86+
__attribute__((unused))
8587
static off_t read_elf64(FILE* fd)
8688
{
8789
Elf64_Ehdr ehdr64;
8890
Elf64_Shdr shdr64;
8991
off_t last_shdr_offset;
90-
off_t ret;
92+
size_t ret;
9193
off_t sht_end, last_section_end;
9294

9395
fseeko(fd, 0, SEEK_SET);
9496
ret = fread(&ehdr64, 1, sizeof(ehdr64), fd);
95-
if (ret < 0 || (size_t)ret != sizeof(ehdr64)) {
97+
if (ret != sizeof(ehdr64)) {
9698
fprintf(stderr, "Read of ELF header from %s failed: %s\n",
9799
fname, strerror(errno));
98100
return -1;
@@ -102,18 +104,18 @@ static off_t read_elf64(FILE* fd)
102104
ehdr.e_shentsize = file16_to_cpu(ehdr64.e_shentsize);
103105
ehdr.e_shnum = file16_to_cpu(ehdr64.e_shnum);
104106

105-
last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1));
107+
last_shdr_offset = (off_t)(ehdr.e_shoff + ((uint64_t)ehdr.e_shentsize * (ehdr.e_shnum - 1)));
106108
fseeko(fd, last_shdr_offset, SEEK_SET);
107109
ret = fread(&shdr64, 1, sizeof(shdr64), fd);
108-
if (ret < 0 || ret != sizeof(shdr64)) {
110+
if (ret != sizeof(shdr64)) {
109111
fprintf(stderr, "Read of ELF section header from %s failed: %s\n",
110112
fname, strerror(errno));
111113
return -1;
112114
}
113115

114116
/* ELF ends either with the table of section headers (SHT) or with a section. */
115-
sht_end = ehdr.e_shoff + (ehdr.e_shentsize * ehdr.e_shnum);
116-
last_section_end = file64_to_cpu(shdr64.sh_offset) + file64_to_cpu(shdr64.sh_size);
117+
sht_end = (off_t)(ehdr.e_shoff + ((uint64_t)ehdr.e_shentsize * ehdr.e_shnum));
118+
last_section_end = (off_t)(file64_to_cpu(shdr64.sh_offset) + file64_to_cpu(shdr64.sh_size));
117119
return sht_end > last_section_end ? sht_end : last_section_end;
118120
}
119121

@@ -155,8 +157,8 @@ bool appimage_get_elf_section_offset_and_length(const char* fname, const char* s
155157
char* strTab = (char*) (data + shdr[elf->e_shstrndx].sh_offset);
156158
for (i = 0; i < elf->e_shnum; i++) {
157159
if (strcmp(&strTab[shdr[i].sh_name], section_name) == 0) {
158-
*offset = shdr[i].sh_offset;
159-
*length = shdr[i].sh_size;
160+
*offset = (unsigned long)shdr[i].sh_offset;
161+
*length = (unsigned long)shdr[i].sh_size;
160162
}
161163
}
162164
} else {
@@ -175,7 +177,7 @@ char* read_file_offset_length(const char* fname, unsigned long offset, unsigned
175177
return NULL;
176178
}
177179

178-
fseek(f, offset, SEEK_SET);
180+
fseek(f, (long)offset, SEEK_SET);
179181

180182
char* buffer = calloc(length + 1, sizeof(char));
181183
fread(buffer, length, sizeof(char), f);
@@ -191,7 +193,7 @@ int appimage_print_hex(const char* fname, unsigned long offset, unsigned long le
191193
return 1;
192194
}
193195

194-
for (long long k = 0; k < length && data[k] != '\0'; k++) {
196+
for (unsigned long k = 0; k < length && data[k] != '\0'; k++) {
195197
printf("%x", data[k]);
196198
}
197199

0 commit comments

Comments
 (0)