Browse Source

Suppress all compilation warnings

Yann Weber 1 year ago
parent
commit
ce496c6e0b
12 changed files with 113 additions and 102 deletions
  1. 1
    1
      Makefile.am
  2. 7
    2
      asm_env.c
  3. 2
    7
      asmsh.c
  4. 8
    12
      compile.c
  5. 44
    2
      completion.c
  6. 2
    39
      completion.h
  7. 2
    0
      history.c
  8. 12
    0
      logger.c
  9. 3
    14
      mmap_parse.c
  10. 8
    4
      shell.c
  11. 15
    3
      shell_cmds.c
  12. 9
    18
      shell_cmds.h

+ 1
- 1
Makefile.am View File

@@ -10,7 +10,7 @@ child_SOURCES = child.s
10 10
 asmsh_LDADD=child_bin.o
11 11
 asmsh_LDFLAGS=-g -O2
12 12
 
13
-AM_CFLAGS=-Wall
13
+AM_CFLAGS=-Wall -Werror
14 14
 AM_CCASFLAGS=-g -O0
15 15
 child_LDFLAGS=-nostdlib
16 16
 

+ 7
- 2
asm_env.c View File

@@ -42,6 +42,7 @@ asmsh_env_t* asmsh_env(const char *childpath)
42 42
 	res->childpath = NULL;
43 43
 	if(childpath && (res->childpath = strdup(childpath)) == NULL)
44 44
 	{
45
+		err=errno;
45 46
 		goto err_pathdup;
46 47
 	}
47 48
 
@@ -53,6 +54,7 @@ asmsh_env_t* asmsh_env(const char *childpath)
53 54
 
54 55
 	if(asmsh_env_update(res) < 0)
55 56
 	{
57
+		err=errno;
56 58
 		goto err;
57 59
 	}
58 60
 	
@@ -89,7 +91,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
89 91
 {
90 92
 	int err;
91 93
 	u_int64_t data;
92
-	char written;
94
+	unsigned char written;
93 95
 	char bleft  = (u_int64_t)addr % 8;
94 96
 
95 97
 	written = 0;
@@ -162,7 +164,7 @@ int asmsh_env_write_code(asmsh_env_t *env, asmsh_bytecode_t *bcode)
162 164
 
163 165
 int asmsh_env_step(asmsh_env_t *env, int *status)
164 166
 {
165
-	int err, ret;
167
+	int err;
166 168
 
167 169
 	if(status) { *status = 0; }
168 170
 
@@ -278,12 +280,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
278 280
 
279 281
 	if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
280 282
 	{
283
+		err=errno;
281 284
 		perror("Unable to attach to child process");
282 285
 		goto err;
283 286
 	}
284 287
 
285 288
 	if(waitpid(env->pid, &wstatus, 0) < 0)
286 289
 	{
290
+		err=errno;
287 291
 		perror("Unable to wait for child process");
288 292
 		goto err;
289 293
 	}
@@ -353,6 +357,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
353 357
 		*/
354 358
 		if(asmsh_env_step(env, NULL))
355 359
 		{
360
+			err=errno;
356 361
 			goto err;
357 362
 		}
358 363
 	}

+ 2
- 7
asmsh.c View File

@@ -30,7 +30,7 @@ int main(int argc, char *argv[], char *envp[])
30 30
 	asmsh_logger_t *logger;
31 31
 	asmsh_t sh;
32 32
 	
33
-	int ret;
33
+	int ret = 0;
34 34
 	char *cmd;
35 35
 	char prompt[64];
36 36
 	bzero(prompt, sizeof(prompt));
@@ -56,7 +56,7 @@ int main(int argc, char *argv[], char *envp[])
56 56
 	{
57 57
 		snprintf(prompt, sizeof(prompt)-1,
58 58
 				"asmsh@%p > ",
59
-				sh.env->regs.rip);
59
+				(void*)sh.env->regs.rip);
60 60
 		cmd = readline(prompt);
61 61
 		if(!cmd)
62 62
 		{
@@ -78,7 +78,6 @@ int main(int argc, char *argv[], char *envp[])
78 78
 		add_history(cmd);
79 79
 	}
80 80
 
81
-end:
82 81
 	asmsh_cleanup(&sh);
83 82
 
84 83
 	HIST_ENTRY **hist = history_list();
@@ -92,8 +91,4 @@ end:
92 91
 	free(hpath);
93 92
 
94 93
 	return ret;
95
-
96
-err:
97
-	ret = 1;
98
-	goto end;
99 94
 }

+ 8
- 12
compile.c View File

@@ -32,20 +32,14 @@ asmsh_asmc_ctx_t* asmsh_asmc_ctx(const char *progname, \
32 32
 	const struct timeval default_timeout = {.tv_sec = 1, .tv_usec = 250000};
33 33
 	asmsh_asmc_ctx_t *res;
34 34
 
35
-	int argc; // args count
36 35
 	int errno_bck; // errno will take this value in err labels
37
-	int tmpfd;
38
-	char *temp_name;
39
-
40
-	int i;
41
-	char * const *ptr;
42 36
 
43 37
 	if((res = malloc(sizeof(*res))) == NULL)
44 38
 	{
45 39
 		errno_bck =  errno;
46 40
 		goto err_alloc_res;
47 41
 	}
48
-	bzero(res, sizeof(res));
42
+	bzero(res, sizeof(*res));
49 43
 
50 44
 	if(asmsh_asmc_buildarg(progname, result_path, args,
51 45
 			&res->args, &res->progname, &res->respath) < 0)
@@ -64,7 +58,6 @@ asmsh_asmc_ctx_t* asmsh_asmc_ctx(const char *progname, \
64 58
 	memcpy(&(res->ctimeout), ctimeout?ctimeout:&default_timeout,
65 59
 			sizeof(struct timeval));
66 60
 
67
-	free(temp_name);
68 61
 	return res;
69 62
 
70 63
 err_buildarg:
@@ -232,7 +225,7 @@ int asmsh_asmc_compile_unsafe(asmsh_asmc_ctx_t *ctx, const char *_instr,
232 225
 	{
233 226
 		FD_ZERO(&sigset);
234 227
 		FD_SET(sigfd, &sigset);
235
-		if(select(sigfd+1, &sigset, NULL, NULL, &ctx->ctimeout) == 0)
228
+		if(select(sigfd+1, &sigset, NULL, NULL, &stimeout) == 0)
236 229
 		{
237 230
 			//compilation timeout t_t
238 231
 			asmsh_log_error("Compilation timeout");
@@ -373,6 +366,7 @@ int asmsh_asmc_spawn(asmsh_asmc_ctx_t *ctx)
373 366
 	{
374 367
 		if(pipe(pipes[i]) == -1)
375 368
 		{
369
+			err=errno;
376 370
 			goto err_pipe;
377 371
 		}
378 372
 	}
@@ -400,7 +394,7 @@ int asmsh_asmc_spawn(asmsh_asmc_ctx_t *ctx)
400 394
 err_fork:
401 395
 	i=1;
402 396
 err_pipe:
403
-	for(i; i>=0; i--)
397
+	for(; i>=0; i--)
404 398
 	{
405 399
 		close(pipes[i][0]);
406 400
 		close(pipes[i][1]);
@@ -456,7 +450,7 @@ if(ptr) {\
456 450
 			break;
457 451
 	}
458 452
 
459
-	ptr = instr;
453
+	ptr = (const unsigned char*)instr;
460 454
 	while(*ptr)
461 455
 	{
462 456
 		switch(*ptr)
@@ -587,7 +581,8 @@ int asmh_asmc_bytecode_from_obj(const char *objfile, asmsh_bytecode_t *bytecode)
587 581
 		}
588 582
 		else
589 583
 		{
590
-			dprintf(2, "Unable to read the %d bytes of bytecode\n");
584
+			dprintf(2, "Unable to read the %d bytes of bytecode\n",
585
+					bytecode_sz);
591 586
 			err = ENODATA;
592 587
 		}
593 588
 		goto err;
@@ -662,6 +657,7 @@ int asmsh_asmc_buildarg(const char* progname, const char *result_tpl, \
662 657
 		(*args_o)[i] = strdup(*tocopy);
663 658
 		if(!(*args_o)[i])
664 659
 		{
660
+			err=errno;
665 661
 			perror("Unable to copy compiler args");
666 662
 			goto err_dupargs;
667 663
 		}

+ 44
- 2
completion.c View File

@@ -32,6 +32,7 @@ char **asmsh_completion(const char *buf, const char *text, int start, int end)
32 32
 		return asmsh_compl_arg(buf, text, start);
33 33
 	}
34 34
 	// command completion handling
35
+	return NULL;
35 36
 }
36 37
 
37 38
 
@@ -286,10 +287,9 @@ err:
286 287
 
287 288
 static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i)
288 289
 {
289
-	int stlen, len, oi, need_cmp;
290
+	int stlen, len, need_cmp;
290 291
 	char buf[32]; /* large enough */
291 292
 	char fmt[32];
292
-	oi = *i;
293 293
 	len = strlen(macro);
294 294
 	stlen = strlen(start);
295 295
 
@@ -419,3 +419,45 @@ static int _completion_macro_match(const char *macro, const char *text, int mlen
419 419
 	return 1;
420 420
 }
421 421
 
422
+#if HAVE_LIBREADLINE == 1
423
+static char **_asmsh_completion_res;
424
+static char **_asmsh_completion_cur;
425
+static int _asmsh_completion_st[2];
426
+extern int rl_attempted_completion_over;
427
+extern const char* rl_special_prefixes;
428
+extern const char* rl_basic_word_break_characters;
429
+char *asmsh_rl_completion_gen(const char *text, int state)
430
+{
431
+	if(!state)
432
+	{
433
+		_asmsh_completion_res = asmsh_completion(rl_line_buffer,
434
+							text,
435
+							_asmsh_completion_st[0],
436
+							_asmsh_completion_st[1]);
437
+		_asmsh_completion_cur = _asmsh_completion_res;
438
+		if(!_asmsh_completion_res)
439
+		{
440
+			return NULL;
441
+		}
442
+	}
443
+	char *res = *_asmsh_completion_cur;
444
+	if(!*_asmsh_completion_cur)
445
+	{
446
+		free(_asmsh_completion_res);
447
+	}
448
+	else
449
+	{
450
+		*_asmsh_completion_cur = NULL;
451
+		_asmsh_completion_cur++;
452
+	}
453
+	return res;
454
+}
455
+char **asmsh_rl_completion(const char *text, int start, int end)
456
+{
457
+	rl_attempted_completion_over = 1;
458
+	_asmsh_completion_st[0]=start;
459
+	_asmsh_completion_st[1]=end;
460
+	return rl_completion_matches(text, asmsh_rl_completion_gen);
461
+}
462
+#endif
463
+

+ 2
- 39
completion.h View File

@@ -59,45 +59,8 @@ struct asmsh_completion_s
59 59
 char **asmsh_completion(const char *buf, const char *text, int start, int end);
60 60
 
61 61
 #if HAVE_LIBREADLINE == 1
62
-static char **_asmsh_completion_res;
63
-static char **_asmsh_completion_cur;
64
-static int _asmsh_completion_st[2];
65
-static char *asmsh_rl_completion_gen(const char *text, int state)
66
-{
67
-	if(!state)
68
-	{
69
-		_asmsh_completion_res = asmsh_completion(rl_line_buffer,
70
-							text,
71
-							_asmsh_completion_st[0],
72
-							_asmsh_completion_st[1]);
73
-		_asmsh_completion_cur = _asmsh_completion_res;
74
-		if(!_asmsh_completion_res)
75
-		{
76
-			return NULL;
77
-		}
78
-	}
79
-	char *res = *_asmsh_completion_cur;
80
-	if(!*_asmsh_completion_cur)
81
-	{
82
-		free(_asmsh_completion_res);
83
-	}
84
-	else
85
-	{
86
-		*_asmsh_completion_cur = NULL;
87
-		_asmsh_completion_cur++;
88
-	}
89
-	return res;
90
-}
91
-extern int rl_attempted_completion_over;
92
-extern const char* rl_special_prefixes;
93
-extern const char* rl_basic_word_break_characters;
94
-static char **asmsh_rl_completion(const char *text, int start, int end)
95
-{
96
-	rl_attempted_completion_over = 1;
97
-	_asmsh_completion_st[0]=start;
98
-	_asmsh_completion_st[1]=end;
99
-	return rl_completion_matches(text, asmsh_rl_completion_gen);
100
-}
62
+char *asmsh_rl_completion_gen(const char *text, int state);
63
+char **asmsh_rl_completion(const char *text, int start, int end);
101 64
 #endif
102 65
 
103 66
 /** Return all possible instructions given a start */

+ 2
- 0
history.c View File

@@ -104,4 +104,6 @@ int load_history(const char *history_path, add_history_f *add_h)
104 104
 		}
105 105
 	}while(readed && readed == wanted);
106 106
 	close(fd);
107
+	return 0;
107 108
 }
109
+

+ 12
- 0
logger.c View File

@@ -118,12 +118,15 @@ int asmsh_logger_strdup(asmsh_logger_t *logger, char **result, size_t *res_sz)
118 118
 	asmsh_log_msg_t *cur;
119 119
 	char *buf_ptr;
120 120
 	int ret;
121
+	int err;
121 122
 
122 123
 	*res_sz = 0;
123 124
 	buf_ptr = *result = malloc(buf_sz = BUF_ALLOC);
124 125
 	if(*result == NULL)
125 126
 	{
127
+		err=errno;
126 128
 		perror("Enable to allocate buffer for log messages");
129
+		errno=err;
127 130
 		return -1;
128 131
 	}
129 132
 
@@ -135,6 +138,14 @@ int asmsh_logger_strdup(asmsh_logger_t *logger, char **result, size_t *res_sz)
135 138
 		{
136 139
 			buf_sz += BUF_ALLOC;
137 140
 			void *tmp = realloc(*result, buf_sz);
141
+			if(!tmp)
142
+			{
143
+				err=errno;
144
+				perror("Unable to strdup logline");
145
+				errno=err;
146
+				return -1;
147
+			}
148
+			result=tmp;
138 149
 		}
139 150
 		else
140 151
 		{
@@ -142,6 +153,7 @@ int asmsh_logger_strdup(asmsh_logger_t *logger, char **result, size_t *res_sz)
142 153
 			cur = cur->nxt;
143 154
 		}
144 155
 	}
156
+	return 0;
145 157
 }
146 158
 
147 159
 int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],

+ 3
- 14
mmap_parse.c View File

@@ -52,12 +52,6 @@ int child_mmap_get(pid_t child_pid, child_mmap_l *maps)
52 52
 
53 53
 	close(maps_fd);
54 54
 	return ret;
55
-
56
-err:
57
-	ret = errno;
58
-	close(maps_fd);
59
-	errno = ret;
60
-	return -1;
61 55
 }
62 56
 
63 57
 int child_mmap_get_fd(int maps_fd, child_mmap_l *maps)
@@ -104,7 +98,6 @@ int child_mmap_get_fd(int maps_fd, child_mmap_l *maps)
104 98
 
105 99
 	if(curmap < maps->size)
106 100
 	{
107
-		dprintf(2, "debug realloc : %d %d\n", curmap, maps->size);
108 101
 		tmp = realloc(maps->maps, sizeof(child_mmap_t) * curmap);
109 102
 		if(!tmp)
110 103
 		{
@@ -126,14 +119,12 @@ err:
126 119
 
127 120
 int child_mmap_parseline(char *line, child_mmap_t *maps)
128 121
 {
129
-	char *ptr, *orig, *endptr;
122
+	char *orig, *endptr;
130 123
 	unsigned long long parsed;
131
-	int errno_bck, cperm;
124
+	int cperm;
132 125
 	size_t i;
133 126
 	int major;
134 127
 
135
-	void **addr_ptr[2] = {&maps->start, &maps->stop};
136
-	const char addr_sep[2] = "- ";
137 128
 	const char perms[3] = "rwx";
138 129
 	const int perms_val[3] = {PROT_READ, PROT_WRITE, PROT_EXEC};
139 130
 
@@ -150,7 +141,7 @@ int child_mmap_parseline(char *line, child_mmap_t *maps)
150 141
 	*line = endptr+1;\
151 142
 }
152 143
 	
153
-	ptr = orig = line;
144
+	orig = line;
154 145
 
155 146
 	parsefield(&line, '-', 16, &maps->start);
156 147
 	parsefield(&line, ' ', 16, &maps->stop);
@@ -207,9 +198,7 @@ int child_mmap_parseline(char *line, child_mmap_t *maps)
207 198
 	return 0;
208 199
 
209 200
 	err_inval:
210
-		errno_bck = errno;
211 201
 		dprintf(2, "Invalid procfs/[pid]/maps content '%s'", orig);
212
-	err:
213 202
 		return -1;
214 203
 }
215 204
 

+ 8
- 4
shell.c View File

@@ -71,9 +71,9 @@ int asmsh_exec(asmsh_t *sh, const char *cmd)
71 71
 		return -1;
72 72
 	}
73 73
 
74
-	int clen, ret;
74
+	int ret;
75 75
 	//lstrip whitespace
76
-	for(cmd; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);
76
+	for(; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);
77 77
 	if(!*cmd) { return 0; }
78 78
 
79 79
 	switch(*cmd)
@@ -144,23 +144,27 @@ static int _compile_step(asmsh_t *sh, const char *cmd)
144 144
 
145 145
 static int _handle_command(asmsh_t *sh, const char *cmd)
146 146
 {
147
-	const int clen = strlen(cmd);
148 147
 	const asmsh_cmd_t *match;
149
-	int ret;
148
+	int ret, err;
150 149
 
151 150
 	asmsh_cmd_args_t *args;
152 151
 
153 152
 	args = asmsh_cmd_parse(cmd);
154 153
 	if(!args)
155 154
 	{
155
+		err=errno;
156 156
 		asmsh_log_error("Parse error"); // TODO better error
157
+		errno=err;
157 158
 		return -1;
158 159
 	}
159 160
 
160 161
 	match = asmsh_cmd_match(args->cmd, strlen(args->cmd));
161 162
 	if(!match)
162 163
 	{
164
+		err=errno;
163 165
 		asmsh_log_error("Unknow command");
166
+		asmsh_cmd_args_free(args);
167
+		errno=err;
164 168
 		return -1;
165 169
 	}
166 170
 	ret = match->cmd(sh, args);

+ 15
- 3
shell_cmds.c View File

@@ -75,7 +75,7 @@ asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd)
75 75
 	strncpy(ret->cmd, cmd, cmdlen);
76 76
 	ret->cmd[cmdlen] = '\0';
77 77
 	// lstrip white chars
78
-	for(ptr; *ptr && (*ptr == ' ' || *ptr == '\t' || *ptr == '\n'); ptr++);
78
+	for(; *ptr && (*ptr == ' ' || *ptr == '\t' || *ptr == '\n'); ptr++);
79 79
 	if(!*ptr)
80 80
 	{
81 81
 		// end of command
@@ -87,7 +87,7 @@ asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd)
87 87
 	do
88 88
 	{
89 89
 		int arglen = 0;
90
-		for(ptr; *ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\n'; ptr++)
90
+		for(; *ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\n'; ptr++)
91 91
 		{
92 92
 			arglen++;
93 93
 		}
@@ -107,7 +107,7 @@ asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd)
107 107
 			goto err;
108 108
 		}
109 109
 		// lstrip white chars
110
-		for(ptr; *ptr && (*ptr == ' ' || *ptr == '\t' || *ptr == '\n'); ptr++);
110
+		for(; *ptr && (*ptr == ' ' || *ptr == '\t' || *ptr == '\n'); ptr++);
111 111
 		arg=ptr;
112 112
 	}while(*ptr);
113 113
 
@@ -121,6 +121,18 @@ err:
121 121
 	return NULL;
122 122
 }
123 123
 
124
+
125
+void asmsh_cmd_args_free(asmsh_cmd_args_t *args)
126
+{
127
+	if(!args) { return; }
128
+	for(char **a=args->args; *a; a++)
129
+	{
130
+		free(*a);
131
+	}
132
+	free(args);
133
+}
134
+
135
+
124 136
 int asmsh_cmd_bcode(asmsh_t *sh, char *argbuf, int bufsz, int argc, char **args)
125 137
 {
126 138
 	asmsh_bytecode_t bcode;

+ 9
- 18
shell_cmds.h View File

@@ -74,16 +74,7 @@ const asmsh_cmd_t *asmsh_cmd_match(const char *cmd, int stop);
74 74
 /** @return NULL on error or ptr on new cmd args */
75 75
 asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd);
76 76
 
77
-static void asmsh_cmd_args_free(asmsh_cmd_args_t *args)
78
-{
79
-	if(!args) { return; }
80
-	for(char **a=args->args; *a; a++)
81
-	{
82
-		free(*a);
83
-	}
84
-	free(args);
85
-}
86
-
77
+void asmsh_cmd_args_free(asmsh_cmd_args_t *args);
87 78
 
88 79
 const char *asmsh_cmd_help(asmsh_t *sh);
89 80
 
@@ -129,12 +120,12 @@ static int _print_regs(asmsh_t *sh, asmsh_cmd_args_t *args)
129 120
 	
130 121
 #define FLG(b, l) ( (r->eflags & (1<<b))?l:'-' )
131 122
 
132
-	printf("rax: %016lx rbx: %016lx rcx: %016lx rdx: %016lx\n\
133
-rbp: %016lx rsi: %016lx rdi: %016lx rsp: %016lx\n\
134
- r8: %016lx  r9: %016lx r10: %016lx r11: %016lx\n\
135
-r12: %016lx r13: %016lx r14: %016lx r15: %016lx\n\
136
-rip: %016lx flg: %016lx\n\
137
-cs: %04x ds: %04x es: %04x fs:%04x gs: %04x ss:%04x\n\
123
+	printf("rax: %016llx rbx: %016llx rcx: %016llx rdx: %016llx\n\
124
+rbp: %016llx rsi: %016llx rdi: %016llx rsp: %016llx\n\
125
+ r8: %016llx  r9: %016llx r10: %016llx r11: %016llx\n\
126
+r12: %016llx r13: %016llx r14: %016llx r15: %016llx\n\
127
+rip: %016llx flg: %016llx\n\
128
+cs: %04llx ds: %04llx es: %04llx fs:%04llx gs: %04llx ss:%04llx\n\
138 129
 flags: %c%c%c%c|%c%c%c\n\
139 130
        ODSZ|APC\n\
140 131
 ",	r->rax, r->rbx, r->rcx, r->rdx,\
@@ -157,6 +148,7 @@ static int _reset(asmsh_t *sh, asmsh_cmd_args_t *args)
157 148
 	asmsh_cleanup(sh);
158 149
 	asmsh_init(sh, childpath);
159 150
 	free(childpath);
151
+	return 0;
160 152
 }
161 153
 
162 154
 // Display CPU flag resister values
@@ -215,10 +207,9 @@ static const asmsh_cmd_t asmsh_CMDS[] = {
215 207
 static int _help(asmsh_t *sh, asmsh_cmd_args_t *args)
216 208
 {
217 209
 	char buf[4096], abuf[64];
218
-	int ret, pret;
210
+	int ret;
219 211
 
220 212
 	ret = snprintf(buf, 4096, "Available commands :\n");
221
-	pret = ret;
222 213
 	for(const asmsh_cmd_t *cmd = asmsh_CMDS; cmd->str; cmd++)
223 214
 	{
224 215
 		snprintf(abuf, 64, "%s %s", cmd->sms, cmd->usage);

Loading…
Cancel
Save