summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2020-12-03 10:29:54 +0100
committerDavid Kalnischkies <david@kalnischkies.de>2021-02-03 17:36:45 +0100
commit03790b071d6a97374a03af36eda5698a143300b0 (patch)
tree49d09c79eb6c05d99072f79dc8fe6ecb6341590e
parent1b3ad3891465f8b72bcedfb4edd513cb74eec7f3 (diff)
Retire and deprecate _strtabexpand
If the Configuration code calling this was any indication, it is hard to use – and even that monster still caused heap-buffer-overflow errors, so instead of trying to fix it, lets just use methods which are far easier to use. The question why this is done at all remains, but is left for another day as an exercise for the reader.
-rw-r--r--apt-pkg/cdrom.cc31
-rw-r--r--apt-pkg/contrib/configuration.cc22
-rw-r--r--apt-pkg/contrib/strutl.h2
3 files changed, 15 insertions, 40 deletions
diff --git a/apt-pkg/cdrom.cc b/apt-pkg/cdrom.cc
index 5df03a99c..aacb78bba 100644
--- a/apt-pkg/cdrom.cc
+++ b/apt-pkg/cdrom.cc
@@ -464,14 +464,12 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
string File = _config->FindFile("Dir::Etc::sourcelist");
- // Open the stream for reading
- ifstream F((FileExists(File)?File.c_str():"/dev/null"),
- ios::in );
- if (F.fail() == true)
- return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
+ FileFd F(FileExists(File) ? File : "/dev/null", FileFd::ReadOnly);
+ if (not F.IsOpen() || F.Failed())
+ return _error->Errno("WriteSourceList", "Opening %s failed", File.c_str());
string NewFile = File + ".new";
- RemoveFile("WriteDatabase", NewFile);
+ RemoveFile("WriteSourceList", NewFile);
ofstream Out(NewFile.c_str());
if (!Out)
return _error->Errno("ofstream::ofstream",
@@ -487,21 +485,16 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
else
Type = "deb";
- char Buffer[300];
int CurLine = 0;
bool First = true;
- while (F.eof() == false)
- {
- F.getline(Buffer,sizeof(Buffer));
- CurLine++;
- if (F.fail() && !F.eof())
- return _error->Error(_("Line %u too long in source list %s."),
- CurLine,File.c_str());
- _strtabexpand(Buffer,sizeof(Buffer));
- _strstrip(Buffer);
-
+ std::string Buffer;
+ while (F.ReadLine(Buffer))
+ {
+ ++CurLine;
+ auto const Cleaned = APT::String::Strip(SubstVar(Buffer, "\t", " "));
+
// Comment or blank
- if (Buffer[0] == '#' || Buffer[0] == 0)
+ if (Cleaned.empty() || Cleaned[0] == '#')
{
Out << Buffer << endl;
continue;
@@ -523,7 +516,7 @@ bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
// Grok it
string cType;
string URI;
- const char *C = Buffer;
+ const char *C = Cleaned.c_str();
if (ParseQuoteWord(C,cType) == false ||
ParseQuoteWord(C,URI) == false)
{
diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc
index 0c98ff304..554307324 100644
--- a/apt-pkg/contrib/configuration.cc
+++ b/apt-pkg/contrib/configuration.cc
@@ -861,26 +861,8 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectio
// The input line with comments stripped.
std::string Fragment;
- // Expand tabs in the input line and remove leading and trailing
- // whitespace.
- {
- const int BufferSize = Input.size() * 8 + 1;
- char *Buffer = new char[BufferSize];
- try
- {
- memcpy(Buffer, Input.c_str(), Input.size() + 1);
-
- _strtabexpand(Buffer, BufferSize);
- _strstrip(Buffer);
- Input = Buffer;
- }
- catch(...)
- {
- delete[] Buffer;
- throw;
- }
- delete[] Buffer;
- }
+ // Expand tabs in the input line and remove leading and trailing whitespace.
+ Input = APT::String::Strip(SubstVar(Input, "\t", " "));
CurLine++;
// Now strip comments; if the whole line is contained in a
diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h
index c25c6208a..8669c97a5 100644
--- a/apt-pkg/contrib/strutl.h
+++ b/apt-pkg/contrib/strutl.h
@@ -42,7 +42,7 @@ namespace APT {
APT_PUBLIC bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
APT_PUBLIC char *_strstrip(char *String);
APT_PUBLIC char *_strrstrip(char *String); // right strip only
-APT_PUBLIC char *_strtabexpand(char *String,size_t Len);
+APT_DEPRECATED_MSG("Use SubstVar to avoid memory headaches") APT_PUBLIC char *_strtabexpand(char *String,size_t Len);
APT_PUBLIC bool ParseQuoteWord(const char *&String,std::string &Res);
APT_PUBLIC bool ParseCWord(const char *&String,std::string &Res);
APT_PUBLIC std::string QuoteString(const std::string &Str,const char *Bad);