Browse Source

Bug fix: Digits-only file mask constraint was interpreted as time constraint instead of size constraint (caused by the Bug 672) + Change: Failing on invalid size constraint

+ Size constraint unit tests

Source commit: bb41faa987ca3fa15c7fe433b7d3357c1841d1ff
Martin Prikryl 9 years ago
parent
commit
33cb2f4010

+ 39 - 0
source/core/Common.cpp

@@ -1635,6 +1635,45 @@ bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime,
   return Result;
 }
 //---------------------------------------------------------------------------
+// Keep consistent with parse_blocksize64
+bool __fastcall TryStrToSize(UnicodeString SizeStr, __int64 & Size)
+{
+  int Index = 0;
+  while ((Index + 1 <= SizeStr.Length()) && IsDigit(SizeStr[Index + 1]))
+  {
+    Index++;
+  }
+  bool Result =
+    (Index > 0) && TryStrToInt64(SizeStr.SubString(1, Index), Size);
+  if (Result)
+  {
+    SizeStr = SizeStr.SubString(Index + 1, SizeStr.Length() - Index).Trim();
+    if (!SizeStr.IsEmpty())
+    {
+      Result = (SizeStr.Length() == 1);
+      if (Result)
+      {
+        wchar_t Unit = (wchar_t)toupper(SizeStr[1]);
+        switch (Unit)
+        {
+          case 'G':
+            Size *= 1024;
+            // fallthru
+          case 'M':
+            Size *= 1024;
+            // fallthru
+          case 'K':
+            Size *= 1024;
+            break;
+          default:
+            Result = false;
+        }
+      }
+    }
+  }
+  return Result;
+}
+//---------------------------------------------------------------------------
 static __int64 __fastcall DateTimeToUnix(const TDateTime DateTime)
 {
   const TDateTimeParams * CurrentParams = GetDateTimeParams(0);

+ 1 - 0
source/core/Common.h

@@ -127,6 +127,7 @@ bool __fastcall IsWine();
 TLibModule * __fastcall FindModule(void * Instance);
 __int64 __fastcall Round(double Number);
 bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime, bool Add);
+bool __fastcall TryStrToSize(UnicodeString SizeStr, __int64 & Size);
 LCID __fastcall GetDefaultLCID();
 UnicodeString __fastcall DefaultEncodingName();
 UnicodeString __fastcall WindowsProductName();

+ 6 - 2
source/core/FileMasks.cpp

@@ -584,7 +584,8 @@ void __fastcall TFileMasks::CreateMask(
       FormatSettings.ShortTimeFormat = "hh:nn:ss";
 
       TDateTime Modification;
-      if (TryStrToDateTime(PartStr, Modification, FormatSettings) ||
+      __int64 DummySize;
+      if ((!TryStrToInt64(PartStr, DummySize) && TryStrToDateTime(PartStr, Modification, FormatSettings)) ||
           TryRelativeStrToDateTime(PartStr, Modification, false))
       {
         TMask::TMaskBoundary & ModificationMask =
@@ -611,7 +612,10 @@ void __fastcall TFileMasks::CreateMask(
         }
 
         SizeMask = Boundary;
-        Size = ParseSize(PartStr);
+        if (!TryStrToSize(PartStr, Size))
+        {
+          ThrowError(PartStart, PartEnd);
+        }
       }
     }
     else if (!PartStr.IsEmpty())

+ 0 - 6
source/core/PuttyIntf.cpp

@@ -664,12 +664,6 @@ void FreeKey(TPrivateKey * PrivateKey)
   sfree(Ssh2Key);
 }
 //---------------------------------------------------------------------------
-__int64 __fastcall ParseSize(UnicodeString SizeStr)
-{
-  AnsiString AnsiSizeStr = AnsiString(SizeStr);
-  return parse_blocksize64(AnsiSizeStr.c_str());
-}
-//---------------------------------------------------------------------------
 bool __fastcall HasGSSAPI(UnicodeString CustomPath)
 {
   static int has = -1;

+ 0 - 2
source/core/PuttyTools.h

@@ -18,8 +18,6 @@ void SaveKey(TKeyType KeyType, const UnicodeString & FileName,
   const UnicodeString & Passphrase, TPrivateKey * PrivateKey);
 void FreeKey(TPrivateKey * PrivateKey);
 //---------------------------------------------------------------------------
-__int64 __fastcall ParseSize(UnicodeString SizeStr);
-//---------------------------------------------------------------------------
 bool __fastcall HasGSSAPI(UnicodeString CustomPath);
 //---------------------------------------------------------------------------
 void __fastcall AES256EncodeWithMAC(char * Data, size_t Len, const char * Password,

+ 2 - 7
source/putty/misc.c

@@ -20,10 +20,10 @@
  * All numbers are decimal, and suffixes refer to powers of two.
  * Case-insensitive.
  */
-__int64 parse_blocksize64(const char *bs)
+unsigned long parse_blocksize(const char *bs)
 {
     char *suf;
-    __int64 r = strtoul(bs, &suf, 10);
+    unsigned long r = strtoul(bs, &suf, 10);
     if (*suf != '\0') {
 	while (*suf && isspace((unsigned char)*suf)) suf++;
 	switch (*suf) {
@@ -44,11 +44,6 @@ __int64 parse_blocksize64(const char *bs)
     return r;
 }
 
-unsigned long parse_blocksize(const char *bs)
-{
-  return (unsigned long)parse_blocksize64(bs);
-}
-
 /*
  * Parse a ^C style character specification.
  * Returns NULL in `next' if we didn't recognise it as a control character,

+ 0 - 3
source/putty/misc.h

@@ -21,9 +21,6 @@
 typedef struct Filename Filename;
 typedef struct FontSpec FontSpec;
 
-#ifdef MPEXT
-__int64 parse_blocksize64(const char *bs);
-#endif
 unsigned long parse_blocksize(const char *bs);
 char ctrlparse(char *s, char **next);