[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to solve : "cast increases required alignment of target type"



ptiJo <ptiJo_(_at_)_noos_(_dot_)_fr> wrote:

> ====== RATEUP.C ======
>     824 static int readhist(file)
>     825 char *file;
>     826 {          
>     827     FILE *fi;
>     828     int i,x, retcode = 0;
>     829     char buf[256];
>     830     struct HISTORY *hist;
>     831     unsigned long rd[5];
>     832     time_t cur;
>     833                
>     834     if ((fi = fopen(file,"r")) != NULL) {
>     835         if (fscanf(fi,"%ld %s %s\n",(long int
> *)&last.time,&last.in[0],&last.out[0
> ]) != 3){

The problem here is that last.time is of type time_t, which expands
to different integer types on different operating systems.

On OpenBSD, time_t is int.  Somewhere else (Linux most likely) it
appears to be long.  This doesn't really matter on 32-bit archs
where sizeof(int) == sizeof(long), but on a 64-bit machine the
format string and the cast above tell scanf() to write a 64-bit
value into a 32-bit variable.  Ouch!

The portable way to solve this is to introduce a temporary variable
of type long, read into that variable, and assign to the variable we
really want to set.

--- rateup.c.orig	Tue Jul 30 01:58:58 2002
+++ rateup.c	Tue Jul 30 02:00:00 2002
@@ -829,13 +829,15 @@
     char buf[256];
     struct HISTORY *hist;
     unsigned long rd[5];
+    long ltime;
     time_t cur;
 
     if ((fi = fopen(file,"r")) != NULL) {
-	if (fscanf(fi,"%ld %s %s\n",(long int *)&last.time,&last.in[0],&last.out[0]) != 3){
+	if (fscanf(fi,"%ld %s %s\n",&ltime,&last.in[0],&last.out[0]) != 3){
            fprintf(stderr,"Read Error: File %s lin 1\n",file);
 	   retcode = 1;
 	}
+	last.time = ltime;
         cur = last.time;
 	x = histvalid=0;
 	hist = history;

-- 
Christian "naddy" Weisgerber                          naddy_(_at_)_mips_(_dot_)_inka_(_dot_)_de



Visit your host, monkey.org