Side issue with TString and Valgrind

Hello,

I was running Valgrind on one of my code just to track down a memory issue and I saw that a TString declaration is triggering avalgrind message that I don’t understand. Can someone advice on this side issue ? I am just curious how to get rid of this message

==688== Memcheck, a memory error detector
==688== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==688== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==688== Command: SignalProcessing *_GWOSC_4KHZ_R1-*.root --input -{}_GWOSC_4KHZ_R1-{}-{}.root --output SP-512-{0}-{1}-{2}.root --center --timegate 32
==688==
==688== Invalid read of size 1
==688==    at 0x49FFA29: patcmp(char const*, unsigned long, unsigned short const*, char const*) (in /home/marcomeyer/Software/root/6.26.04/lib/libCore.so)
==688==    by 0x4A0032F: Matchs(char const*, unsigned long, unsigned short const*, char const**) (in /home/marcomeyer/Software/root/6.26.04/lib/libCore.so)
==688==    by 0x4A5104F: TRegexp::Index(TString const&, int*, int) const (in /home/marcomeyer/Software/root/6.26.04/lib/libCore.so)
==688==    by 0x5511FC7: UIUC::TFileReader::ExpandWildcardUrl(TString, char const*) (TFileReader.cc:1039)
==688==    by 0x551150E: UIUC::TFileReader::ExpandWildcardUrl(std::vector<TString, std::allocator<TString> >, char const*) (TFileReader.cc:966)
==688==    by 0x5510124: UIUC::TFileReader::ExpandTextFile(std::vector<TString, std::allocator<TString> >, char const*, int) (TFileReader.cc:861)
==688==    by 0x550F231: UIUC::TFileReader::ExpandWildcard(TString, char const*, int) (TFileReader.cc:802)
==688==    by 0x55084AE: UIUC::TFileReader::Add(TString) (TFileReader.cc:222)
==688==    by 0x55078DD: UIUC::TFileReader::Init(TString) (TFileReader.cc:181)
==688==    by 0x54ADC15: UIUC::HandboxUsage::AddArgument(UIUC::TFileReader*, TString, bool) (HandboxUsage.cc:1946)
==688==    by 0x12A1B3: main (SignalProcessing.C:76)
==688==  Address 0x107403df is 1 bytes before a block of size 32 alloc'd
==688==    at 0x483E217: operator new[](unsigned long) (vg_replace_malloc.c:640)
==688==    by 0x4A53C29: TString::Init(int, int) (in /home/marcomeyer/Software/root/6.26.04/lib/libCore.so)
==688==    by 0x4A53D57: TString::TString(char const*) (in /home/marcomeyer/Software/root/6.26.04/lib/libCore.so)
==688==    by 0x5511F89: UIUC::TFileReader::ExpandWildcardUrl(TString, char const*) (TFileReader.cc:1039)
==688==    by 0x551150E: UIUC::TFileReader::ExpandWildcardUrl(std::vector<TString, std::allocator<TString> >, char const*) (TFileReader.cc:966)
==688==    by 0x5510124: UIUC::TFileReader::ExpandTextFile(std::vector<TString, std::allocator<TString> >, char const*, int) (TFileReader.cc:861)
==688==    by 0x550F231: UIUC::TFileReader::ExpandWildcard(TString, char const*, int) (TFileReader.cc:802)
==688==    by 0x55084AE: UIUC::TFileReader::Add(TString) (TFileReader.cc:222)
==688==    by 0x55078DD: UIUC::TFileReader::Init(TString) (TFileReader.cc:181)
==688==    by 0x54ADC15: UIUC::HandboxUsage::AddArgument(UIUC::TFileReader*, TString, bool) (HandboxUsage.cc:1946)
==688==    by 0x12A1B3: main (SignalProcessing.C:76)
==688==

The code responsible for this is the following:
https://gitlab.cern.ch/escalade/lib/UIUC/-/blob/master/src/TFileReader.cc#L1039

Cheers,
Marco

ROOT Version. v.6.26/04

Hi @meyerma ,

are you passing the ROOT suppression file to valgrind (--suppressions=$ROOTSYS/etc/valgrind-root.supp?

Cheers,
Enrico

Sorry I forgot to tell the command I was running… yes I do !

valgrind --leak-check=full --num-callers=50 --track-origins=yes --suppressions=$ROOTSYS/etc/valgrind-root.supp [...]

Alright, thanks!

In the code you linked to, what overload of TString::Index do you want to invoke? I see two that take a const char * as first parameter, but neither matches the rest of the arguments (see docs).

In fact I tried to reproduce the issue but this code, which is supposed to just gather the relevant logic from the code you linked, does not compile because the invocation of Index is invalid – so I’m a bit confused :slight_smile:

/tmp root-config --version
6.26/03
/tmp cat tmp.cpp
#include <TString.h>

int main() {
  TString filename0 = "somestring";
  int index = -1, length = -1;
  TString rgx(filename0.Prepend("^").Append("$").Data());
  rgx.Index("\\*", &length, index);
}

but

/tmp rootcompile tmp.cpp
g++ -g -Wall -Wextra -Wpedantic -o "tmp" "tmp.cpp" $(root-config --cflags --libs)
tmp.cpp: In function ‘int main()’:
tmp.cpp:7:20: error: invalid conversion from ‘int*’ to ‘Ssiz_t’ {aka ‘int’} [-fpermissive]
    7 |   rgx.Index("\\*", &length, index);
      |                    ^~~~~~~
      |                    |
      |                    int*
In file included from tmp.cpp:1:
/home/blue/ROOT/v626/cmake-build-foo/include/TString.h:639:52: note:   initializing argument 2 of ‘Ssiz_t TString::Index(const char*, Ssiz_t, ECaseCompare) const’
  639 | inline Ssiz_t TString::Index(const char *s, Ssiz_t i, ECaseCompare cmp) const
      |                                             ~~~~~~~^
tmp.cpp:7:29: error: invalid conversion from ‘int’ to ‘TString::ECaseCompare’ [-fpermissive]
    7 |   rgx.Index("\\*", &length, index);
      |                             ^~~~~
      |                             |
      |                             int
/home/blue/ROOT/v626/cmake-build-foo/include/TString.h:639:68: note:   initializing argument 3 of ‘Ssiz_t TString::Index(const char*, Ssiz_t, ECaseCompare) const’
  639 | inline Ssiz_t TString::Index(const char *s, Ssiz_t i, ECaseCompare cmp) const
      |  

P.S.

if you intend to call this:

#include <TString.h>
#include <TRegexp.h>

int main() {
  TString filename0 = "somestring";
  int index = -1, length = -1;
  TString rgx(filename0.Prepend("^").Append("$").Data());
  rgx.Index(TRegexp("\\*"), &length, index);
}

then if I understand the docs correctly passing an index of -1 is wrong, because that’s “the offset at which the matching should start.” (see docs).

Cheers,
Enrico

1 Like

Hi Enrico,

Indeed, I intended to use:

Ssiz_t 	Index (const TRegexp &pat, Ssiz_t *ext, Ssiz_t i=0) const

Then, I think I initialized length with -1 for no reason… :sweat_smile:

Changing index and length to 0 did the job !

 int index = 0;
 Ssiz_t length = 0;

Thank you so much Enrico :slight_smile:

1 Like