summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2020-05-13 09:07:19 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2021-02-03 17:36:46 +0100
commite0743a85c5f5f2f83d91c305450e8ba192194cd8 (patch)
tree831e7cd937102f0e46b494387de31449b75447c2 /apt-pkg/contrib
parent6630c3e5b6af77205b043208ef15720cf270075c (diff)
Forbid negative values in unsigned StrToNum explicitly
strtoul(l) surprises us with parsing negative values which should not exist in the places we use to parse them, so we can just downright refuse them rather than trying to work with them by having them promoted to huge positive values.
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/strutl.cc41
1 files changed, 16 insertions, 25 deletions
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index 7beb5f621..84e79122e 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/apt-pkg/contrib/strutl.cc
@@ -23,6 +23,7 @@
#include <algorithm>
#include <array>
#include <iomanip>
+#include <limits>
#include <locale>
#include <sstream>
#include <sstream>
@@ -1139,34 +1140,24 @@ bool FTPMDTMStrToTime(const char* const str,time_t &time)
/*}}}*/
// StrToNum - Convert a fixed length string to a number /*{{{*/
// ---------------------------------------------------------------------
-/* This is used in decoding the crazy fixed length string headers in
+/* This is used in decoding the crazy fixed length string headers in
tar and ar files. */
bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base)
{
- char S[30];
- if (Len >= sizeof(S))
+ unsigned long long BigRes;
+ if (not StrToNum(Str, BigRes, Len, Base))
return false;
- memcpy(S,Str,Len);
- S[Len] = 0;
-
- // All spaces is a zero
- Res = 0;
- unsigned I;
- for (I = 0; S[I] == ' '; I++);
- if (S[I] == 0)
- return true;
-
- char *End;
- Res = strtoul(S,&End,Base);
- if (End == S)
+
+ if (std::numeric_limits<unsigned long>::max() < BigRes)
return false;
-
+
+ Res = BigRes;
return true;
}
/*}}}*/
// StrToNum - Convert a fixed length string to a number /*{{{*/
// ---------------------------------------------------------------------
-/* This is used in decoding the crazy fixed length string headers in
+/* This is used in decoding the crazy fixed length string headers in
tar and ar files. */
bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base)
{
@@ -1175,20 +1166,20 @@ bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base
return false;
memcpy(S,Str,Len);
S[Len] = 0;
-
+
// All spaces is a zero
Res = 0;
unsigned I;
- for (I = 0; S[I] == ' '; I++);
+ for (I = 0; S[I] == ' '; ++I);
if (S[I] == 0)
return true;
-
+ if (S[I] == '-')
+ return false;
+
char *End;
+ errno = 0;
Res = strtoull(S,&End,Base);
- if (End == S)
- return false;
-
- return true;
+ return not (End == S || errno != 0);
}
/*}}}*/