Solved

Data Overflow

Posted on 2006-10-19
10
238 Views
Last Modified: 2010-04-05
I am looking at a delphi routine and it seems the delphi routine would create an Overflow. What happens in Delphi when a variable overflows. Does it error out or does delphi truncate the data some how? I am using Delphi 7.
0
Comment
Question by:sk33v3
  • 5
  • 5
10 Comments
 
LVL 28

Expert Comment

by:ciuly
ID: 17770138
it is usually ignored.
but I think it could depend also on how you configured the compiler: project options, compiler: overflow checking.
I jsut made a quick test
var b:byte;
begin
  b:=255;
  b:=b+1;
  showmessage(inttostr(b));
end;
with that checked and unchecked, I always got 0 with no error. so I guess not all overflows generate errors.
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17770203
Well I am trying to Convert a delphi routine into another programming language and when I try to convert I keep getting overflow issues.

Cache := (b + Cache) * FirstCache + LastCache;

Cache is a Word variable type
b is a byte variable type
FirstCache is a constant with the value of 32768
and Last Cache is a constant with the value of 16382

Now from my understanding of that line if b+cache results in anything larger than 1 a buffer overflow would result. Is that correct?
0
 
LVL 28

Expert Comment

by:ciuly
ID: 17771866
that is correct
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17773507
Ok, so on every round Cache gets set back to 0 because fo the buffer overflow? When compiled the application seems to work just fine and I know that b does get larger than 1.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 17773759
not 0. either firstcache + LastCache or LastCache
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Author Comment

by:sk33v3
ID: 17773793
Is there a way to predict which one it is going to get?
0
 
LVL 28

Expert Comment

by:ciuly
ID: 17773838
well ... I guess yes
if b+cache mod 2 = 0 then LastCache
else
firstcache+LastCache
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17775935
Even though the actual line of code doesn't have the mod in it delphi assumes mod 2?
0
 
LVL 28

Accepted Solution

by:
ciuly earned 500 total points
ID: 17776274
oh no. you asked for a prediction. that is the prediction.

if b+cache = 0 then Cache := 0 * FirstCache + LastCache; => lastcache
if b+cache = 1 then Cache := 1 * FirstCache + LastCache; => firstcache+lastcache
if b+cache = 2 then Cache := 2 * FirstCache + LastCache; => lastcache
if b+cache = 3 then Cache := 3 * FirstCache + LastCache; => firstcache+lastcache
and so on
from there we see that because firstcache is constant and has the value 32768, at every second case, it will overflow.( 2k*32768 = k*65536, for each k from 0 to n and since 65536 is 10000000000000000 in binary (1 followed by 16 0's) it results that all even b+cache will cause the multiplication to overflow and thus leaving only lastcache in the equation and on the other hand, all even b+cache will also cause an overflow, but will leave one instance of firstcache so it will result in firstcache+lastcache)
so from there the prediction:
if (b+cache) mod 2 = 0 then LastCache
else firstcache+LastCache
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17777127
Ok, that is what I wanted to know. Thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now