Porting to x32 ABI, fix a few warnings

Send your patches here
Post Reply
marekm
Posts: 4
Joined: 09 Jun 2015, 11:01

Porting to x32 ABI, fix a few warnings

Post by marekm » 23 Jun 2015, 23:59

I'm playing with the unofficial Debian jessie port to x32 ABI http://www.debian-x32.org . One caveat is that time_t is 64-bit to avoid year 2038 bugs (they are still in the RADIUS protocol itself though), while long is 32-bit just like pointers; these types shouldn't be assumed to be the same. I also fixed a few possibly unsafe sprintf and mktemp calls in the process - see below for a patch. It's a start, not tested in production yet, just compiles with no warnings.

Code: Select all

diff --git a/accel-pppd/cli/std_cmd.c b/accel-pppd/cli/std_cmd.c
index 38e6a8b..41b2d92 100644
--- a/accel-pppd/cli/std_cmd.c
+++ b/accel-pppd/cli/std_cmd.c
@@ -21,7 +21,7 @@ static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt,
 {
 	struct timespec ts;
 	time_t dt;
-	int day,hour;
+	int day, hour, m, s;
 	char statm_fname[128];
 	FILE *f;
 	unsigned long vmsize = 0, vmrss = 0;
@@ -43,8 +43,10 @@ static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt,
 	dt %= 60 * 60 * 24;
 	hour = dt / (60 * 60);
 	dt %= 60 * 60;
+	m = dt / 60;
+	s = dt % 60;
 
-	cli_sendv(client, "uptime: %i.%02i:%02lu:%02lu\r\n", day, hour, dt / 60, dt % 60);
+	cli_sendv(client, "uptime: %i.%02i:%02i:%02i\r\n", day, hour, m, s);
 	cli_sendv(client, "cpu: %i%%\r\n", triton_stat.cpu);
 #ifdef MEMDEBUG
 	cli_send(client,  "memory:\r\n");
diff --git a/accel-pppd/extra/pppd_compat.c b/accel-pppd/extra/pppd_compat.c
index 482a708..33c7c63 100644
--- a/accel-pppd/extra/pppd_compat.c
+++ b/accel-pppd/extra/pppd_compat.c
@@ -145,14 +145,14 @@ static void ev_ses_pre_up(struct ap_session *ses)
 
 #ifdef RADIUS
 	if (pd->tmp_fname) {
-		char *fname = _malloc(PATH_MAX);
+		int i;
+		char *fname;
 
-		if (!fname) {
+		i = _asprintf(&fname, "%s.%s", conf_radattr_prefix, ses->ifname);
+		if (i == -1) {
 			log_emerg("pppd_compat: out of memory\n");
 			return;
 		}
-
-		sprintf(fname, "%s.%s", conf_radattr_prefix, ses->ifname);
 		rename(pd->tmp_fname, fname);
 
 		_free(fname);
@@ -373,26 +373,26 @@ static void ev_radius_coa(struct ev_radius_t *ev)
 static void remove_radattr(struct pppd_compat_pd *pd)
 {
 	char *fname;
+	int i;
 
 	if (pd->tmp_fname) {
 		unlink(pd->tmp_fname);
 		_free(pd->tmp_fname);
 	} else {
-		fname = _malloc(PATH_MAX);
-		if (!fname) {
+		i = _asprintf(&fname, "%s.%s", conf_radattr_prefix, pd->ses->ifname);
+		if (i == -1) {
 			log_emerg("pppd_compat: out of memory\n");
 			return;
 		}
-
-		sprintf(fname, "%s.%s", conf_radattr_prefix, pd->ses->ifname);
-
 		if (unlink(fname)) {
 			log_ppp_warn("pppd_compat: failed to remove '%s': %s\n", fname, strerror(errno));
 		}
-		sprintf(fname, "%s_old.%s", conf_radattr_prefix, pd->ses->ifname);
-		unlink(fname);
-
 		_free(fname);
+		i = _asprintf(&fname, "%s_old.%s", conf_radattr_prefix, pd->ses->ifname);
+		if (i != -1) {
+			unlink(fname);
+			_free(fname);
+		}
 	}
 }
 
@@ -406,33 +406,34 @@ static void write_radattr(struct pppd_compat_pd *pd, struct rad_packet_t *pack)
 	int i;
 	in_addr_t addr;
 
-	fname1 = _malloc(PATH_MAX);
-	if (!fname1) {
-		log_emerg("pppd_compat: out of memory\n");
-		return;
-	}
-
 	if (ses->state == AP_STATE_ACTIVE) {
-		fname2 = _malloc(PATH_MAX);
-		if (!fname2) {
+		i = _asprintf(&fname1, "%s.%s", conf_radattr_prefix, ses->ifname);
+		if (i == -1) {
+			log_emerg("pppd_compat: out of memory\n");
+			return;
+		}
+		i = _asprintf(&fname2, "%s_old.%s", conf_radattr_prefix, ses->ifname);
+		if (i == -1) {
 			log_emerg("pppd_compat: out of memory\n");
 			_free(fname1);
 			return;
 		}
-	}
-
-	if (ses->state == AP_STATE_ACTIVE) {
-		sprintf(fname1, "%s.%s", conf_radattr_prefix, ses->ifname);
-		sprintf(fname2, "%s_old.%s", conf_radattr_prefix, ses->ifname);
 		if (rename(fname1, fname2)) {
 			log_ppp_warn("pppd_compat: rename: %s\n", strerror(errno));
 		}
+		f = fopen(fname1, "w");
 	} else {
-		sprintf(fname1, "%s.XXXXXX", conf_radattr_prefix);
-		mktemp(fname1);
+		int fd;
+
+		i = _asprintf(&fname1, "%s.XXXXXX", conf_radattr_prefix);
+		if (i == -1) {
+			log_emerg("pppd_compat: out of memory\n");
+			return;
+		}
+		fd = mkstemp(fname1);
+		f = (fd >= 0) ? fdopen(fd, "w") : NULL;
 	}
 
-	f = fopen(fname1, "w");
 	if (f) {
 		list_for_each_entry(attr, &pack->attrs, entry) {
 			fprintf(f, "%s ", attr->attr->name);
@@ -457,7 +458,7 @@ static void write_radattr(struct pppd_compat_pd *pd, struct rad_packet_t *pack)
 					fprintf(f, "%i.%i.%i.%i\n", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
 					break;
 				case ATTR_TYPE_DATE:
-					fprintf(f, "%lu\n", attr->val.date);
+					fprintf(f, "%" PRIu64 "\n", (uint64_t) attr->val.date);
 					break;
 			}
 		}
@@ -549,7 +550,7 @@ static void fill_env(char **env, char *mem, struct pppd_compat_pd *pd)
 
 	if (pd->ses->stop_time) {
 		env[n++] = mem;
-		mem += sprintf(mem, "CONNECT_TIME=%lu", pd->ses->stop_time - pd->ses->start_time) + 1;
+		mem += sprintf(mem, "CONNECT_TIME=%lu", (unsigned long)(pd->ses->stop_time - pd->ses->start_time)) + 1;
 		env[n++] = mem;
 		mem += sprintf(mem, "BYTES_SENT=%" PRIu64, tx_bytes) + 1;
 		env[n++] = mem;

Dmitry
Администратор
Posts: 949
Joined: 09 Oct 2014, 10:06

Re: Porting to x32 ABI, fix a few warnings

Post by Dmitry » 30 Nov 2015, 16:23

please send your patch as attachment
because all tabulations are mangled

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest