Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is this considered truncation

350 views
Skip to first unread message

cbfal...@my-dejanews.com

unread,
Nov 12, 1998, 3:00:00 AM11/12/98
to
I have the following line in a source:

if (fseek(dspfile,
(long) (2 * sizeof(int) * stringcount),
SEEK_CUR))
ACTION();

and the (long) (...) line generates (in lint)

Warning 647: suspicious truncation

Can anyone tell me why? stringcount is an unsigned int.

--
Chuck Falconer (Charles_...@NOSPAMapsnet.com)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
--
comp.lang.c.moderated - cl...@plethora.net

Alan Shutko

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
>>>>> "C" == cbfalconer <cbfal...@my-dejanews.com> writes:

C> and the (long) (...) line generates (in lint)

C> Warning 647: suspicious truncation

C> Can anyone tell me why? stringcount is an unsigned int.

Right. And if long and int are the same size, unsigned int can hold
larger numbers than signed int (ie, long).

--
Alan Shutko <a...@acm.org> - By consent of the corrupted
The sooner you fall behind, the more time you have to catch up.
--
comp.lang.c.moderated - cl...@plethora.net

Dan Pop

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
In <clcm-1998...@plethora.net> cbfal...@my-dejanews.com writes:

>I have the following line in a source:
>
> if (fseek(dspfile,
> (long) (2 * sizeof(int) * stringcount),
> SEEK_CUR))
> ACTION();
>

>and the (long) (...) line generates (in lint)
>

> Warning 647: suspicious truncation


>
>Can anyone tell me why? stringcount is an unsigned int.

Most likely, because size_t is defined as unsigned long on your platform.
Or merely because size_t *can* be defined as unsigned long.

If it is unsigned long, the type of the expression 2 * sizeof(int) *
stringcount is unsigned long and lint warns about converting an
unsigned long value to long. If ULONG_MAX > LONG_MAX, the conversion is
not guaranteed to produce the correct result.

BTW, you don't have to cast that expression to long, the compiler will
do it for you (unless you're using a K&R C compiler).

Dan
--
Dan Pop
CERN, IT Division
Email: Dan...@cern.ch
Mail: CERN - EP, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--
comp.lang.c.moderated - cl...@plethora.net

Mitch

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to

cbfal...@my-dejanews.com wrote in article
<clcm-1998...@plethora.net>...


> I have the following line in a source:
>
> if (fseek(dspfile,
> (long) (2 * sizeof(int) * stringcount),
> SEEK_CUR))
> ACTION();
>
> and the (long) (...) line generates (in lint)
>
> Warning 647: suspicious truncation
>
> Can anyone tell me why? stringcount is an unsigned int.

I suspect want you really want is:

if (fseek(dspfile, 2 * sizeof(int) * (long) stringcount, SEEK_CUR))
ERROR_ACTION();

an int times an int (when in the original parentheses) might have already
overflowed before the cast to (long). I think that is what lint is telling
you.

Mitch
--
comp.lang.c.moderated - cl...@plethora.net

Robert O'Dowd

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
cbfal...@my-dejanews.com wrote:
>
> I have the following line in a source:
>
> if (fseek(dspfile,
> (long) (2 * sizeof(int) * stringcount),
> SEEK_CUR))
> ACTION();
>
> and the (long) (...) line generates (in lint)
>
> Warning 647: suspicious truncation
>
> Can anyone tell me why? stringcount is an unsigned int.

At a guess, because sizeof returns a size_t. Let's say, for
sake of argument, size_t maps to a unsigned long. Then
2*sizeof(int)*stringcount will have a type of unsigned long.
That would be a suspicious conversion as some values that
can be represented in an unsigned long won't fit in a long.

[Your milage will vary depending on what the actual definition
of size_t on your system is, what lint assumes, etc etc].



-<Automagically included trailer>
Robert O'Dowd Ph +61 (8) 9553 3618
DSTO Bldg A51 Fax +61 (8) 9553 3577
HMAS Stirling Email:
robert...@dsto.defence.gov.au
Rockingham, Western Australia, 6958

Disclaimer: Opinions above are mine and may be worth what you paid for
them
--
comp.lang.c.moderated - cl...@plethora.net

Jack Klein

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
On Thu, 12 Nov 1998 18:06:31 GMT, cbfal...@my-dejanews.com wrote:

> I have the following line in a source:
>
> if (fseek(dspfile,
> (long) (2 * sizeof(int) * stringcount),
> SEEK_CUR))
> ACTION();
>
> and the (long) (...) line generates (in lint)
>
> Warning 647: suspicious truncation
>
> Can anyone tell me why? stringcount is an unsigned int.
>

> --
> Chuck Falconer (Charles_...@NOSPAMapsnet.com)
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

<Jack>

The type of a value generated by the sizeof operator is size_t. The actual
type which your compiler uses is implementation defined, but it must be an
unsigned type. On many implementations it is typedef'd to unsigned long.
When this expression is evaluated, the constant 2 and the current value of the
integer stringcount are promoted to the type of size_t, and the result is of
this type.

If size_t is unsigned long, the result is unsigned long and casting it to a
signed long as you are doing results in a possible loss of data.

</Jack>
--
comp.lang.c.moderated - cl...@plethora.net

V. Raghavendra Holla.

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
hi,
u better do,

if (fseek(dspfile,
((long) 2 * sizeof(int) * stringcount),
SEEK_CUR))

because expression '2*sizeof(int)*stringcount' will give u integer value
where truncation may occur, and u are casting it with long. Since all
expression are evaluted ,results are kept in temp memory and () has
heighest precedence than (long) compiler genarates Error.

-
V.Raghavendra Holla.
Raghaven...@bosch.com

| I have the following line in a source:
|
| if (fseek(dspfile,
| (long) (2 * sizeof(int) * stringcount),
| SEEK_CUR))
| ACTION();
|
| and the (long) (...) line generates (in lint)
|
| Warning 647: suspicious truncation
|
| Can anyone tell me why? stringcount is an unsigned int.
|
| --
| Chuck Falconer (Charles_...@NOSPAMapsnet.com)
|
| -----------== Posted via Deja News, The Discussion Network ==----------
| http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

| --
| comp.lang.c.moderated - cl...@plethora.net
|
--
comp.lang.c.moderated - cl...@plethora.net

Eric Amick

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
cbfal...@my-dejanews.com wrote:
> I have the following line in a source:

> if (fseek(dspfile,
> (long) (2 * sizeof(int) * stringcount),
> SEEK_CUR))
> ACTION();

> and the (long) (...) line generates (in lint)

> Warning 647: suspicious truncation

> Can anyone tell me why? stringcount is an unsigned int.

There are two possible explanations I can see. First, if sizeof(long int)
== sizeof(int), casting an unsigned int result to long could truncate the
value. Second, sizeof returns a size_t, which is an unsigned type. If
size_t is defined as an unsigned long int on your system, the result would
be unsigned long int and the cast could truncate the value as well.

--
Eric Amick
Columbia, MD
eam...@clark.net
--
comp.lang.c.moderated - cl...@plethora.net

TechnoCrate

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
On Thu, 12 Nov 1998 18:06:31 GMT, cbfal...@my-dejanews.com wrote:

>I have the following line in a source:
>
> if (fseek(dspfile,
> (long) (2 * sizeof(int) * stringcount),
> SEEK_CUR))
> ACTION();
>
>and the (long) (...) line generates (in lint)
>
> Warning 647: suspicious truncation
>
>Can anyone tell me why? stringcount is an unsigned int.
>

Well, I can only see two possible explanations:

1- long and int are of the same size and ,in this case, converting
from unsigned int to the signed long involves possible truncation.

2- size_t is not an unsigned int (hardly plausible but you can look it
up in stddef.h)

Besides, why bother? My dark mentor told me warnings are normal and
good since they don't break off compilation ;-)
--
comp.lang.c.moderated - cl...@plethora.net

Mitch

unread,
Nov 16, 1998, 3:00:00 AM11/16/98
to

Mitch <no...@nowhere.com> wrote in article
<clcm-1998...@plethora.net>...


> I suspect want you really want is:
>
> if (fseek(dspfile, 2 * sizeof(int) * (long) stringcount, SEEK_CUR))
> ERROR_ACTION();
>
> an int times an int (when in the original parentheses) might have already
> overflowed before the cast to (long). I think that is what lint is
telling
> you.

Of course, that would only apply in situations where the 'int' type is
shorter than a 'long' (i.e. 2 bytes as oppose to 4)
See Dan's post above, for the actual reason.

MItch
--
comp.lang.c.moderated - cl...@plethora.net

cbfal...@my-dejanews.com

unread,
Nov 16, 1998, 3:00:00 AM11/16/98
to
In article <clcm-1998...@plethora.net>,
Dan...@cern.ch (Dan Pop) wrote:

> In <clcm-1998...@plethora.net> cbfal...@my-dejanews.com writes:
>
> >I have the following line in a source:
> >
> > if (fseek(dspfile,
> > (long) (2 * sizeof(int) * stringcount),
> > SEEK_CUR))
> > ACTION();
> >
> >and the (long) (...) line generates (in lint)
> >
> > Warning 647: suspicious truncation
> >
> >Can anyone tell me why? stringcount is an unsigned int.
>
> Most likely, because size_t is defined as unsigned long on your platform.
> Or merely because size_t *can* be defined as unsigned long.
>
> If it is unsigned long, the type of the expression 2 * sizeof(int) *
> stringcount is unsigned long and lint warns about converting an
> unsigned long value to long. If ULONG_MAX > LONG_MAX, the conversion is
> not guaranteed to produce the correct result.

I checked the headers, and size_t is defined as unsigned int. Someone said it
was probably lint ignoring the specific implementation and just worrying
about sizeof portabilities in general. Obviously the result of sizeof(int)
is never going to stretch the capacity of an int :-)

>
> BTW, you don't have to cast that expression to long, the compiler will
> do it for you (unless you're using a K&R C compiler).

You got it - exactly the problem. Although the compiler has an ANSI option,
the code has to work with lots of unconverted legacy code, and the libraries
for the ANSI and K&R are incompatible. This is the TXO system for Verifone
terminals.

>
> Dan
> --
> Dan Pop
> CERN, IT Division
> Email: Dan...@cern.ch
> Mail: CERN - EP, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland

cbfal...@my-dejanews.com

unread,
Nov 16, 1998, 3:00:00 AM11/16/98
to
In article <clcm-1998...@plethora.net>,
"Mitch" <nu...@zippo.com> wrote:
>
> cbfal...@my-dejanews.com wrote in article
> <clcm-1998...@plethora.net>...

> > I have the following line in a source:
> >
> > if (fseek(dspfile,
> > (long) (2 * sizeof(int) * stringcount),
> > SEEK_CUR))
> > ACTION();
> >
> > and the (long) (...) line generates (in lint)
> >
> > Warning 647: suspicious truncation
> >
> > Can anyone tell me why? stringcount is an unsigned int.
>
> I suspect want you really want is:
>
> if (fseek(dspfile, 2 * sizeof(int) * (long) stringcount, SEEK_CUR))
> ERROR_ACTION();
>
> an int times an int (when in the original parentheses) might have already
> overflowed before the cast to (long). I think that is what lint is telling
> you.
>

Thanks - now that makes sense to me. I'll try that out. Actually I
deliberately wrote it the other way because integer multiplication is much
faster than long multiplication on this platform.

Dan Pop

unread,
Nov 18, 1998, 3:00:00 AM11/18/98
to
In <clcm-1998...@plethora.net> cbfal...@my-dejanews.com writes:

>> >I have the following line in a source:
>> >
>> > if (fseek(dspfile,
>> > (long) (2 * sizeof(int) * stringcount),
>> > SEEK_CUR))
>> > ACTION();
>> >
>> >and the (long) (...) line generates (in lint)
>> >
>> > Warning 647: suspicious truncation
>> >
>> >Can anyone tell me why? stringcount is an unsigned int.
>>

>> Most likely, because size_t is defined as unsigned long on your platform.
>> Or merely because size_t *can* be defined as unsigned long.
>>
>> If it is unsigned long, the type of the expression 2 * sizeof(int) *
>> stringcount is unsigned long and lint warns about converting an
>> unsigned long value to long. If ULONG_MAX > LONG_MAX, the conversion is
>> not guaranteed to produce the correct result.
>
>I checked the headers, and size_t is defined as unsigned int. Someone said it
>was probably lint ignoring the specific implementation and just worrying
>about sizeof portabilities in general. Obviously the result of sizeof(int)
>is never going to stretch the capacity of an int :-)

You missed the point. For all that lint knows, the result of
2 * sizeof(int) * stringcount may very well stress the capacity of long,
if int and long have the same size. Because the type of this expression
is unsigned int on your implementation (assuming that stringcount is not
long). This is what lint tries to tell you.

0 new messages