diff --git a/core/base/src/TString.cxx b/core/base/src/TString.cxx
index a4f2b65b0cebbdc9e30aad8bde715fc246d7aefb..2da3df363014c9a170f52d460c845e0716bf0715 100644
--- a/core/base/src/TString.cxx
+++ b/core/base/src/TString.cxx
@@ -125,6 +125,10 @@ TString::TString(const std::string &s)
 
 TString::TString(const char *cs, Ssiz_t n)
 {
+   if (n < 0) {
+      Error("TString::TString", "Negative length!");
+      return;
+   }
    char *data = Init(n, n);
    memcpy(data, cs, n);
 }
@@ -204,6 +208,14 @@ TString::TString(const TSubString& substr)
 
 TString::TString(const char *a1, Ssiz_t n1, const char *a2, Ssiz_t n2)
 {
+   if (n1 < 0) {
+      Error("TString::TString", "Negative first length!");
+      return;
+   }
+   if (n2 < 0) {
+      Error("TString::TString", "Negative second length!");
+      return;
+   }
    if (!a1) n1=0;
    if (!a2) n2=0;
    Ssiz_t tot = n1+n2;
@@ -850,6 +862,10 @@ static int MemIsEqual(const char *p, const char *q, Ssiz_t n)
 Ssiz_t TString::Index(const char *pattern, Ssiz_t plen, Ssiz_t startIndex,
                       ECaseCompare cmp) const
 {
+   if (plen < 0) {
+      Error("TString::Index", "Negative first pattern length!");
+      return kNPOS;
+   }
    Ssiz_t slen = Length();
    if (slen < startIndex + plen) return kNPOS;
    if (plen == 0) return startIndex;
@@ -920,7 +936,8 @@ Bool_t TString::MaybeWildcard() const
 
 TString& TString::Prepend(char c, Ssiz_t rep)
 {
-   if (!rep) return *this;
+   if (rep <= 0)
+      return *this;
 
    Ssiz_t len = Length();
    Ssiz_t tot = len + rep;  // Final string length
@@ -967,6 +984,14 @@ TString &TString::Replace(Ssiz_t pos, Ssiz_t n1, const char *cs, Ssiz_t n2)
             "first argument out of bounds: pos = %d, Length = %d", pos, len);
       return *this;
    }
+   if (n1 < 0) {
+      Error("TString::Replace", "Negative number of characters to remove!");
+      return *this;
+   }
+   if (n2 < 0) {
+      Error("TString::Replace", "Negative number of replacement characters!");
+      return *this;
+   }
 
    n1 = TMath::Min(n1, len - pos);
    if (!cs) n2 = 0;