Jump to content
dmitrybv

TIniFile.ReadString, TIniFile.WriteString value length limit.

Recommended Posts

Posted (edited)

I wonder why TIniFile.ReadString sets the maximum value size to 2047 without checking the actual value in the file and without limiting the value size in TIniFile.WriteString.

 

Stackoverflow accepted the maximum allowed value to be 65,535

https://stackoverflow.com/questions/10507927/getprivateprofilestring-buffer-length

 

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestringw

DWORD GetPrivateProfileStringW(
  [in]  LPCWSTR lpAppName,
  [in]  LPCWSTR lpKeyName,
  [in]  LPCWSTR lpDefault,
  [out] LPWSTR  lpReturnedString,
  [in]  DWORD   nSize,
  [in]  LPCWSTR lpFileName
)

...
If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one.

 

But if the developers of System.IniFiles.TIniFile implemented a limit of 2047, then why did they do it only for TIniFile.ReadString (without throwing an exception when exceeded), but not for TIniFile.WriteString.

 

 


 

unit System.IniFiles

...

function TIniFile.ReadString(const Section, Ident, Default: string): string;
var
  Buffer: array[0..2047] of Char;
begin
                                                                                                                   
  SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section),
    MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer),
    MarshaledString(FFileName)));
end;

procedure TIniFile.WriteString(const Section, Ident, Value: string);
begin
  if not WritePrivateProfileString(MarshaledString(Section), MarshaledString(Ident),
                                   MarshaledString(Value), MarshaledString(FFileName)) then
    raise EIniFileException.CreateResFmt(@SIniFileWriteError, [FileName]);
end;

 

Edited by dmitrybv
  • Like 2

Share this post


Link to post
On 10/8/2024 at 3:36 AM, dmitrybv said:

But if the developers of System.IniFiles.TIniFile implemented a limit of 2047, then why did they do it only for TIniFile.ReadString (without throwing an exception when exceeded), but not for TIniFile.WriteString.

They probably thought that 2047 was "enough for everybody" and didn't think of it as a limit, so they didn't impose a limit on WriteString either.

But we can only guess.

 

OTOH nobody(*1) uses TIniFile any more but TMemIniFile because it overcomes all the limitations of Get/SetPrivateProfileString so that's a moot point.

 

(*1 for suitable definitions of nobody )

  • Like 3

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×