Skip to content

Commit

Permalink
Fixed many Unicode buffer bugs: WinAPI calls with "SizeOf(array[] of …
Browse files Browse the repository at this point in the history
…Char)" instead of Length()
  • Loading branch information
ahausladen committed Jul 31, 2016
1 parent c8abb56 commit 5d433a2
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 92 deletions.
17 changes: 10 additions & 7 deletions jvcl/run/JvAni.pas
Expand Up @@ -672,7 +672,7 @@ procedure TJvAni.ReadAniStream(Stream: TStream);
Icon: TIcon;
IsIcon: Boolean;
HotSpot: TPoint;
Buffer: array [0..255] of Char;
Buffer: array [0..255] of AnsiChar;
begin
{ Make sure it's a RIFF ANI file }
if not ReadTag(Stream, Tag) or (Tag.ckID <> FOURCC_RIFF) then
Expand Down Expand Up @@ -755,19 +755,19 @@ procedure TJvAni.ReadAniStream(Stream: TStream);
if Tag.ckID = FOURCC_INAM then
begin
if (cbChunk < Tag.ckSize) or
not ReadChunkN(Stream, Tag, Buffer[0], SizeOf(Buffer) - 1) then
not ReadChunkN(Stream, Tag, Buffer[0], Length(Buffer) - 1) then
Break;
Dec(cbChunk, PadUp(Tag.ckSize));
FTitle := Buffer;
FTitle := string(Buffer);
end
else
if Tag.ckID = FOURCC_IART then
begin
if (cbChunk < Tag.ckSize) or
not ReadChunkN(Stream, Tag, Buffer[0], SizeOf(Buffer) - 1) then
not ReadChunkN(Stream, Tag, Buffer[0], Length(Buffer) - 1) then
Break;
Dec(cbChunk, PadUp(Tag.ckSize));
FAuthor := Buffer;
FAuthor := string(Buffer);
end
else
begin
Expand Down Expand Up @@ -849,6 +849,7 @@ procedure TJvAni.WriteAniStream(Stream: TStream);
MemStream: TMemoryStream;
TagRIFF, TagLIST, Tag: TJvAniTag;
ID: TJvFourCC;
AnsiTitle, AnsiAuthor: AnsiString;
begin
MemStream := TMemoryStream.Create;
try
Expand All @@ -865,13 +866,15 @@ procedure TJvAni.WriteAniStream(Stream: TStream);
if Title <> '' then
begin
StartWriteChunk(MemStream, Tag, FOURCC_INAM);
MemStream.Write(PChar(Title)^, Length(Title) + 1);
AnsiTitle := AnsiString(Title);
MemStream.Write(PAnsiChar(AnsiTitle)^, Length(AnsiTitle) + 1);
EndWriteChunk(MemStream, Tag, 0);
end;
if Author <> '' then
begin
StartWriteChunk(MemStream, Tag, FOURCC_IART);
MemStream.Write(PChar(Author)^, Length(Author) + 1);
AnsiAuthor := AnsiString(Author);
MemStream.Write(PAnsiChar(AnsiAuthor)^, Length(AnsiAuthor) + 1);
EndWriteChunk(MemStream, Tag, 0);
end;
EndWriteChunk(MemStream, TagLIST, 0);
Expand Down
6 changes: 3 additions & 3 deletions jvcl/run/JvAppRegistryStorage.pas
Expand Up @@ -268,7 +268,7 @@ procedure TJvAppRegistryStorage.EnumFolders(const Path: string; const Strings: T
try
I := 0;
repeat
EnumRes := RegEnumKey(TmpHKEY, I, SubKeyName, SizeOf(SubKeyName));
EnumRes := RegEnumKey(TmpHKEY, I, SubKeyName, Length(SubKeyName));
if (EnumRes = ERROR_SUCCESS) and (not ReportListAsValue or
not ListStored(Path + RegPathDelim + SubKeyName)) then
Strings.Add(SubKeyName);
Expand All @@ -291,7 +291,7 @@ procedure TJvAppRegistryStorage.EnumValues(const Path: string; const Strings: TS
TmpHKEY: HKEY;
I: Integer;
Name: array [0..511] of Char;
NameLen: Cardinal;
NameLen: DWORD;
EnumRes: Longint;
begin
PathIsList := ReportListAsValue and ListStored(Path);
Expand All @@ -305,7 +305,7 @@ procedure TJvAppRegistryStorage.EnumValues(const Path: string; const Strings: TS
try
I := 0;
repeat
NameLen := SizeOf(Name);
NameLen := Length(Name);
EnumRes := RegEnumValue(TmpHKEY, I, Name, NameLen, nil, nil, nil, nil);
if (EnumRes = ERROR_SUCCESS) and (not PathIsList or (not AnsiSameText(cCount, Name) and
not NameIsListItem(Name))) then
Expand Down
4 changes: 2 additions & 2 deletions jvcl/run/JvCopyError.pas
Expand Up @@ -94,11 +94,11 @@ constructor TJvCopyError.Create(AOwner: TComponent);
function TJvCopyError.Execute: TJvDiskRes;
var
Required: DWORD;
Res: array [0..255] of Char;
Res: array [0..MAX_PATH] of Char;
begin
case SetupCopyError(OwnerWindow, PCharOrNil(Title), PCharOrNil(DiskName),
PChar(PathToSource), PChar(SourceFile), PCharOrNil(TargetFile),
FWin32ErrorCode, JvDiskStylesToDWORD(Style), Res, SizeOf(Res), @Required) of
FWin32ErrorCode, JvDiskStylesToDWORD(Style), Res, Length(Res), @Required) of
DPROMPT_SUCCESS:
begin
FNewPath := Res;
Expand Down
4 changes: 2 additions & 2 deletions jvcl/run/JvDiskPrompt.pas
Expand Up @@ -91,11 +91,11 @@ constructor TJvDiskPrompt.Create(AOwner: TComponent);
function TJvDiskPrompt.Execute: TJvDiskRes;
var
Required: DWORD;
Res: array [0..255] of Char;
Res: array [0..MAX_PATH] of Char;
begin
case SetupPromptForDisk(OwnerWindow, Pointer(Title), Pointer(DiskName),
Pointer(PathToSource), PChar(FileSought), Pointer(TagFile),
JvDiskStylesToDWORD(Style), Res, SizeOf(Res), Required) of
JvDiskStylesToDWORD(Style), Res, Length(Res), Required) of
DPROMPT_SUCCESS:
begin
FNewPath := Res;
Expand Down
2 changes: 1 addition & 1 deletion jvcl/run/JvDockSupportControl.pas
Expand Up @@ -3509,7 +3509,7 @@ function TJvDockTabStrings.Get(Index: Integer): string;
begin
TCItem.mask := TCIF_TEXT or RTL[FTabControl.UseRightToLeftReading];
TCItem.pszText := Buffer;
TCItem.cchTextMax := SizeOf(Buffer);
TCItem.cchTextMax := Length(Buffer);
if SendMessage(FTabControl.Handle, TCM_GETITEM, Index, LPARAM(@TCItem)) = 0 then
TabControlError(Format(sTabFailRetrieve, [Index]));
Result := Buffer;
Expand Down
2 changes: 1 addition & 1 deletion jvcl/run/JvDriveCtrls.pas
Expand Up @@ -521,7 +521,7 @@ procedure TJvDriveCombo.BuildList;
LastErrorMode := SetErrorMode(SEM_NOOPENFILEERRORBOX);
try
FillChar(Tmp[0], SizeOf(Tmp), #0);
GetLogicalDriveStrings(SizeOf(Tmp), Tmp);
GetLogicalDriveStrings(Length(Tmp) - 1, Tmp);
P := Tmp;
while P^ <> #0 do
begin
Expand Down
10 changes: 5 additions & 5 deletions jvcl/run/JvExceptionForm.pas
Expand Up @@ -158,14 +158,14 @@ procedure TJvErrorDialog.ErrorInfo(var LogicalAddress: Pointer; var ModuleName:
begin
VirtualQuery(ExceptAddr, Info, SizeOf(Info));
if (Info.State <> MEM_COMMIT) or
(GetModuleFileName(THandle(Info.AllocationBase), Temp, SizeOf(Temp)) = 0) then
(GetModuleFileName(THandle(Info.AllocationBase), Temp, Length(Temp)) = 0) then
begin
GetModuleFileName(HInstance, Temp, SizeOf(Temp));
GetModuleFileName(HInstance, Temp, Length(Temp));
LogicalAddress := ConvertAddr(LogicalAddress);
end
else
INT_PTR(LogicalAddress) := INT_PTR(LogicalAddress) - INT_PTR(Info.AllocationBase);
StrLCopy(ModName, AnsiStrRScan(Temp, PathDelim) + 1, SizeOf(ModName) - 1);
StrLCopy(ModName, AnsiStrRScan(Temp, PathDelim) + 1, Length(ModName) - 1);
ModuleName := StrPas(ModName);
end;
{$ENDIF MSWINDOWS}
Expand All @@ -174,9 +174,9 @@ procedure TJvErrorDialog.ErrorInfo(var LogicalAddress: Pointer; var ModuleName:
var
Temp, ModName: array [0..MAX_PATH] of Char;
begin
GetModuleFileName(HInstance, Temp, SizeOf(Temp));
GetModuleFileName(HInstance, Temp, Length(Temp));
LogicalAddress := ConvertAddr(LogicalAddress);
StrLCopy(ModName, AnsiStrRScan(Temp, PathDelim) + 1, SizeOf(ModName) - 1);
StrLCopy(ModName, AnsiStrRScan(Temp, PathDelim) + 1, Length(ModName) - 1);
ModuleName := StrPas(ModName);
end;
{$ENDIF UNIX}
Expand Down
4 changes: 2 additions & 2 deletions jvcl/run/JvGnugettext.pas
Expand Up @@ -998,7 +998,7 @@ function SysUtilsEnumStringModules(Instance: {$IFDEF DELPHI2012OROLDER}NativeInt
begin
with PStrData(Data)^ do begin
SetString(Str, Buffer,
LoadString(Instance, Ident, @Buffer[0], sizeof(Buffer)));
LoadString(Instance, Ident, @Buffer[0], Length(Buffer)));
Result := Str = '';
end;
end;
Expand Down Expand Up @@ -2461,7 +2461,7 @@ function TGnuGettextInstance.LoadResString(
if not Win32PlatformIsUnicode then begin
SetString(Result, Buffer,
LoadString(FindResourceHInstance(ResStringRec.Module^),
ResStringRec.Identifier, Buffer, SizeOf(Buffer)))
ResStringRec.Identifier, Buffer, Length(Buffer)))
end else begin
Result := '';
Len := 0;
Expand Down
21 changes: 9 additions & 12 deletions jvcl/run/JvJCLUtils.pas
Expand Up @@ -4820,7 +4820,7 @@ function FormatLongDate(Value: TDateTime): string;
begin
DateTimeToSystemTime(Value, SystemTime);
SetString(Result, Buffer, GetDateFormat(GetThreadLocale, DATE_LONGDATE,
@SystemTime, nil, Buffer, SizeOf(Buffer) - 1));
@SystemTime, nil, Buffer, Length(Buffer) - 1));
Result := TrimRight(Result);
end;
{$ENDIF MSWINDOWS}
Expand Down Expand Up @@ -5991,16 +5991,16 @@ function DeleteFilesEx(const FileMasks: array of string): Boolean;

function GetWindowsDir: string;
var
Buffer: array [0..MAX_PATH] of Char;
Buffer: array [0..MAX_PATH - 1] of Char;
begin
SetString(Result, Buffer, GetWindowsDirectory(Buffer, SizeOf(Buffer)));
SetString(Result, Buffer, GetWindowsDirectory(Buffer, Length(Buffer) - 1));
end;

function GetSystemDir: string;
var
Buffer: array [0..MAX_PATH] of Char;
Buffer: array [0..MAX_PATH - 1] of Char;
begin
SetString(Result, Buffer, GetSystemDirectory(Buffer, SizeOf(Buffer)));
SetString(Result, Buffer, GetSystemDirectory(Buffer, Length(Buffer) - 1));
end;

{$ENDIF MSWINDOWS}
Expand Down Expand Up @@ -6723,7 +6723,7 @@ procedure ShowStartButton(Visible: Boolean);
Child := GetWindow(Tray, GW_CHILD);
while Child <> 0 do
begin
if GetClassName(Child, C, SizeOf(C)) > 0 then
if GetClassName(Child, C, Length(C)) > 0 then
begin
S := StrPas(C);
if UpperCase(S) = 'BUTTON' then
Expand Down Expand Up @@ -6895,11 +6895,10 @@ function EnumWindowsProc(Handle: THandle; LParam: TStrings): Boolean; stdcall;
begin
if Windows.IsWindowVisible(Handle) then
begin
GetWindowText(Handle, St, SizeOf(St));
GetWindowText(Handle, St, Length(St));
St2 := St;
if St2 <> '' then
with TStrings(LParam) do
AddObject(St2, TObject(Handle));
TStrings(LParam).AddObject(St2, TObject(Handle));
end;
Result := True;
end;
Expand Down Expand Up @@ -7871,11 +7870,9 @@ function WindowClassName(Wnd: THandle): string;
var
Buffer: array [0..255] of Char;
begin
SetString(Result, Buffer, GetClassName(Wnd, Buffer, SizeOf(Buffer) - 1));
SetString(Result, Buffer, GetClassName(Wnd, Buffer, Length(Buffer) - 1));
end;



function GetAnimation: Boolean;
var
Info: TAnimationInfo;
Expand Down
6 changes: 1 addition & 5 deletions jvcl/run/JvLabel.pas
Expand Up @@ -702,10 +702,6 @@ procedure TJvCustomLabel.DoDrawText(var Rect: TRect; Flags: Integer);
DoDrawCaption(Rect, Flags);
end;


//
// TODO: check if code for VCL is applicable to CLX. If so, make change
//
procedure TJvCustomLabel.DrawAngleText(var Rect: TRect; Flags: Word; HasImage: Boolean;
ShadowSize: Byte; ShadowColor: TColorRef; ShadowPos: TShadowPosition);
var
Expand All @@ -720,7 +716,7 @@ procedure TJvCustomLabel.DrawAngleText(var Rect: TRect; Flags: Word; HasImage: B
begin
Angle10 := Angle * 10;
CalcRect := (Flags and DT_CALCRECT <> 0);
StrLCopy(@Text, PChar(GetLabelCaption), SizeOf(Text) - 1);
StrLCopy(@Text, PChar(GetLabelCaption), Length(Text) - 1);
if CalcRect and ((Text[0] = #0) or ShowAccelChar and
(Text[0] = '&') and (Text[1] = #0)) then
StrCopy(Text, ' ');
Expand Down
4 changes: 2 additions & 2 deletions jvcl/run/JvListView.pas
Expand Up @@ -761,7 +761,7 @@ procedure TJvListItem.SetGroupId(const Value: Integer);
List := Owner.Owner;
if Assigned(List) then
begin
ZeroMemory(@Infos, sizeof(Infos));
ZeroMemory(@Infos, SizeOf(Infos));
Infos.mask := LVIF_GROUPID;
Infos.iItem := Index;
Infos.iGroupId := FGroupId;
Expand Down Expand Up @@ -1789,7 +1789,7 @@ function TJvListView.GetColumnIndex(PHeader: PNMHdr): Integer;
FillChar(ItemInfo, SizeOf(ItemInfo), 0);
ItemInfo.Mask := HDI_TEXT;
ItemInfo.pszText := Buffer;
ItemInfo.cchTextMax := SizeOf(Buffer) - 1;
ItemInfo.cchTextMax := Length(Buffer) - 1;
Header_GetItem(HwndHeader, ItemIndex, ItemInfo);
if CompareStr(Columns[ItemIndex].Caption, ItemInfo.pszText) = 0 then
begin
Expand Down
2 changes: 1 addition & 1 deletion jvcl/run/JvMenus.pas
Expand Up @@ -3408,7 +3408,7 @@ procedure TJvXPMenuItemPainter.DrawItemBorderParts(Item: TMenuItem;
ItemRect: TRect;
LocalWRect: TRect;
begin
ItemInfo.cbSize := sizeof(ItemInfo);
ItemInfo.cbSize := SizeOf(ItemInfo);
ItemInfo.fMask := MIIM_STATE;
if GetMenuItemInfo(Item.Parent.Handle, Item.MenuIndex, True, ItemInfo) then
begin
Expand Down
6 changes: 3 additions & 3 deletions jvcl/run/JvNTEventLog.pas
Expand Up @@ -449,7 +449,7 @@ function TJvNTEventLogRecord.GetMessageText: string;
// (rom) memory leak fixed
if FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_ARGUMENT_ARRAY,
Pointer(DllModule), ID, 0, Buffer, SizeOf(Buffer), Args) > 0 then
Pointer(DllModule), ID, 0, Buffer, Length(Buffer), Args) > 0 then
begin
Buffer[StrLen(Buffer) - 2] := #0;
St := Buffer;
Expand Down Expand Up @@ -518,8 +518,8 @@ function TJvNTEventLogRecord.GetUsername: string;
UserSID := SID;
if Assigned(UserSID) then
begin
UserNameLen := SizeOf(UserName);
DomainNameLen := SizeOf(DomainName);
UserNameLen := Length(UserName);
DomainNameLen := Length(DomainName);
if LookupAccountSID(nil, UserSID, UserName, UserNameLen, DomainName, DomainNameLen, Use) then
Result := string(DomainName) + '\' + string(UserName);
end
Expand Down
6 changes: 3 additions & 3 deletions jvcl/run/JvPatchForm.pas
Expand Up @@ -146,8 +146,8 @@ procedure TPatchFrm.OkBtnClick(Sender: TObject);
begin
Caption := Format(RsJvPatcherEditorComparingFilesd, [iCount div j]);
Application.ProcessMessages;
res1 := Src.Read(buf1, sizeof(buf1)); // original file
res2 := Dest.Read(buf2, sizeof(buf2)); // patched file
res1 := Src.Read(buf1, SizeOf(buf1)); // original file
res2 := Dest.Read(buf2, SizeOf(buf2)); // patched file
if res1 = res2 then
begin
for i := 0 to res1 - 1 do
Expand Down Expand Up @@ -203,7 +203,7 @@ procedure TPatchFrm.OkBtnClick(Sender: TObject);
//adding the rest of the file
while Dest.Position < Dest.Size do
begin
res2 := Dest.Read(buf2, sizeof(buf2));
res2 := Dest.Read(buf2, SizeOf(buf2));
for i := 0 to res2 - 1 do
FPatch.Add(Char(Crypt(buf2[i])));
end;
Expand Down
18 changes: 5 additions & 13 deletions jvcl/run/JvRichEdit.pas
Expand Up @@ -2698,7 +2698,7 @@ procedure TJvCustomRichEdit.CMBiDiModeChanged(var Msg: TMessage);
HandleNeeded; { we REALLY need the handle for BiDi }
inherited;

BiDiOptions.cbSize := sizeof(BiDiOptions);
BiDiOptions.cbSize := SizeOf(BiDiOptions);
BiDiOptions.wMask := BOM_NEUTRALOVERRIDE or BOM_CONTEXTREADING or BOM_CONTEXTALIGNMENT;
BiDiOptions.wEffects := BOE_NEUTRALOVERRIDE or BOE_CONTEXTREADING or BOE_CONTEXTALIGNMENT;
SendMessage(Handle, EM_SETBIDIOPTIONS, 0, LPARAM(@BiDiOptions));
Expand Down Expand Up @@ -3588,13 +3588,7 @@ procedure TJvCustomRichEdit.InsertObject(const OleClassName: string;
function TJvCustomRichEdit.InsertObjectDialog: Boolean;
var
Data: TOleUIInsertObject;
{$IFDEF UNICODE}
{ Mantis #4738: OleUIInsertObjectW() returns with OLEUI_IOERR_LPCLSIDEXCLUDEINVALID }
{ Probably windows error; cchFile must be exactly MAXPATH }
NameBuffer: array[0..MAX_PATH div SizeOf(Char) - 1] of Char;
{$ELSE}
NameBuffer: array[0..255] of Char;
{$ENDIF UNICODE}
NameBuffer: array[0..MAX_PATH - 1] of Char; // MSDN: cchFile must not exceed MAX_PATH
OleClientSite: IOleClientSite;
Storage: IStorage;
OleObject: IOleObject;
Expand All @@ -3618,7 +3612,7 @@ function TJvCustomRichEdit.InsertObjectDialog: Boolean;
IOF_CREATENEWOBJECT or IOF_CREATEFILEOBJECT or IOF_CREATELINKOBJECT;
hWndOwner := Handle;
lpszFile := NameBuffer;
cchFile := SizeOf(NameBuffer);
cchFile := Length(NameBuffer);
iid := IOleObject;
oleRender := OLERENDER_DRAW;
lpIOleClientSite := OleClientSite;
Expand Down Expand Up @@ -3651,11 +3645,9 @@ function TJvCustomRichEdit.InsertObjectDialog: Boolean;
OleCheck(IRichEditOle(FRichEditOle).InsertObject(ReObject));
SendMessage(Handle, EM_EXSETSEL, 0, LPARAM(@Selection));
SendMessage(Handle, Messages.EM_SCROLLCARET, 0, 0);
IRichEditOle(FRichEditOle).SetDvaspect(
Longint(REO_IOB_SELECTION), ReObject.dvAspect);
IRichEditOle(FRichEditOle).SetDvaspect(Longint(REO_IOB_SELECTION), ReObject.dvAspect);
if IsNewObject then
OleObject.DoVerb(OLEIVERB_SHOW, nil,
OleClientSite, 0, Handle, ClientRect);
OleObject.DoVerb(OLEIVERB_SHOW, nil, OleClientSite, 0, Handle, ClientRect);
finally
ReleaseObject(OleObject);
end;
Expand Down
2 changes: 1 addition & 1 deletion jvcl/run/JvSecretPanel.pas
Expand Up @@ -425,7 +425,7 @@ procedure TJvSecretPanel.PaintText;
if I = Lines.Count then
StrCopy(STmp, ' ')
else
StrPLCopy(STmp, Lines[I], SizeOf(STmp) - 1);
StrPLCopy(STmp, Lines[I], Length(STmp) - 1);
if R.Top >= RectHeight(FTxtRect) then
Break
else
Expand Down

0 comments on commit 5d433a2

Please sign in to comment.