Subject: Re: some code assumes sizeof(char *) == sizeof(int)
To: None <eeh@netbsd.org>
From: Shin'ichiro TAYA <taya@sm.sony.co.jp>
List: tech-pkg
Date: 02/22/2001 12:50:16
From: eeh@netbsd.org
Subject: Re: some code assumes sizeof(char *) == sizeof(int)
Date: 21 Feb 2001 17:17:57 -0000
> What's a `SAVED_VAR'?
/* Structure describing a saved variable and the value to restore it to.
If a cleanup function is set to restore_variable, the `arg' pointer
points to this. */
typedef struct {
int *variable;
char *desired_setting;
int size;
} SAVED_VAR;
> What's the type of `sv->variable'?
int *variable;
this is a bash-2.04 source code. please refer it for detal.
> The correct fix is probably to change the macro to:
>
> #define unwind_protect_int(X) \
> unwind_protect_var(&(X), (char *)(unsigned long)(X), sizeof(X))
>
> (The `unsigned' is optional, but correctly converts 32-bit pointers
> to 64-bit pointers. But then we're probably not dealing with pointers
> anyway.)
I'll test this later.
But this doesn't fix warning at:
*(sv->variable) = (int)sv->desired_setting;
because sv->desired_setting is char *, converting 64bit value to 32bit value.
> You want to use `sizeof(X)' rather than `sizeof(int)' so you can handle
> both `int' and `long', since the borken code means that they probably
> did not distinguish the two.
well, as macro name says, `long' will never passed to
unwind_protect_int(X), but this is good idea.
> Also: `*(sv->variable) = (int)sv->desired_setting;' is dangerous in
> this setting since it's not clear what the type of `sv->variable' is.
> You may want to add code to handle the standard scalar, and possibly
> floating point, types individually. That way you won't need to
> malloc() something for variables smaller than 8 (or 16) bytes.
sv->variable is used as `int', if sizeof arg is 4, otherwise
used as a pointer to a memory block alloced by malloc.