?
Solved

Strange DupeString AV

Posted on 2004-11-15
7
Medium Priority
?
369 Views
Last Modified: 2010-04-04
Hi, I've made some Office 2003 - like color pickers (hexagon & hsl) and integrated them in a dialog component, now I'm making another dialog for my html-editor app. In this dialog users can manually enter hex color values. Here's the function I use to format (fix) the entered text so it can be properly converted from an html color value to a TColor value:

function TMoreColorsWin.FormatHTMLColor(S: string): string;

procedure ReplaceWrongChars(var s: string);
var
 i: integer;
begin
if s <> '' then
 for i := 1 to 6 do
  if not (s[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']) then
   s[i] := '0';
end;

var
 c: string;
begin
 c := '';
 if s <> '' then
  begin
   c := AnsiReplaceText(s, '#', '');
   ReplaceWrongChars(c);
   if Length(c) = 6 then
    Result := c
   else
    begin
     if Length(c) > 6 then
      c := Copy(c, 1, 6);
     if Length(c) < 6 then
      c := DupeString('0', 6 - Length(c)) + c;  //AV here!!!
     Result := c;
    end;
  end
 else
  Result := '000000';
end;

Everything works fine while "s" is more than 3 chars long, if it's length is 1, 2, 3 (eg "A") i get an AV. Here's how I use the function above :

(ENew: TEdit, NewSwatch - something like TShape)

procedure TMoreColorsWin.ENewKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
 s: string;
begin
if (Key = VK_RETURN) and ENew.Focused then
 begin
  s := FormatHTMLColor(ENew.Text);
  ENew.Text := s;
  NewSwatch.Color := HexToTColor(s);
 end;
end;

I've tried everything and I just can't get rid of the AV, what should I do???
0
Comment
Question by:Bindza
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12587028
Don't know what DupeString is, heh, but I guess you could replace it with something like:

while Length(c) < 6 do
  c := '0' + c;

also, ReplaceWrongChars could be rewritten as follows (for brevity)

procedure ReplaceWrongChars(var s: string);
var
 i: integer;
begin
if s <> '' then
 for i := 1 to 6 do
  if not (s[i] in ['0' .. '9', 'A' .. 'F']) then
   s[i] := '0';
end;
0
 
LVL 6

Expert Comment

by:rbohac
ID: 12587108
Intereting, I can't get this to duplicate.
0
 
LVL 6

Expert Comment

by:rbohac
ID: 12587111
DupeString is contained in StrUtils
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:rbohac
ID: 12587123
You haven't changed the DupeString function at all, have you?
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 12587462
Instead of this:

  if Length(c) < 6 then
      c := DupeString('0', 6 - Length(c)) + c;  //AV here!!!

try this:

While Length(c) < 6 do c:= '0'+c;

regards
Pierre
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 1000 total points
ID: 12587659
The reason for your Access violation is in your ReplaceWrongChars function. You iterate for i:= 1 to 6 in your function therefore if the string parameter you pass to the function is anything less than 6 you will get an AV because you are trying to access e.g. s[4] when only up to s[3] was allocated memory for.

Try this:

function FormatHTMLColor(S: string): string;
var i: integer;
begin
  result:= StringReplace(s, '#','', [rfReplaceAll]);

  if Length(s) > 6
    then SetLength(result, 6)
    else while length(result) < 6 do result:= '0'+result;

  //by now you can be sure the result string is 6 chars long so
  //iterating from 1 to 6 shouldn't give you any AV's
  for i := 1 to 6 do
    if not (result[i] in ['0'..'9', 'A'..'F'])
      then result[i] := '0';
end;

Regards
Pierre
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 12587684
sorry, second line should be

  "if Length(result) > 6"  INSTEAD OF "if Length(s) > 6"

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

864 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