# Question about the function "Form" that return a format string

**URL:** <https://root-forum.cern.ch/t/question-about-the-function-form-that-return-a-format-string/53815>\
**Category:** Newbie\
**Created:** [February 28, 2023, 11:39am UTC](https://root-forum.cern.ch/t/question-about-the-function-form-that-return-a-format-string/53815 "2023-02-28T11:39:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [February 28, 2023, 11:39am UTC](https://root-forum.cern.ch/t/question-about-the-function-form-that-return-a-format-string/53815/1 "2023-02-28T11:39:39Z")

</div>

Hello, I would want to ask about the [Form](https://root.cern.ch/doc/master/TString_8cxx_source.html#l02468) function in ROOT.  
I read from the TString class reference page that this function is not very safe to format strings, as the string can be overwritten. I encountered such issue once or twice.

From my understanding, the function uses `vsnprintf` to create a formatted string with the correct number of characters, then return the string as a char pointer. This approach is quite similar to a post on [stack-overflow](https://stackoverflow.com/a/26221725), which I modify a bit as shown below:

```auto
template <typename ... Args>
char* strformat (
		const char* format,
		Args ... args )
{
	int required_length = 1 + std::snprintf( nullptr, 0, format, args ... );
	if( required_length<1 )
		return nullptr;
	unsigned int string_length = required_length;
	char* return_string = (char*)malloc( required_length * sizeof(char) );
	std::snprintf( return_string, string_length, format, args ... );
	return return_string;
}

```

I don’t understand why the latter function (`strformat`) doesn’t seem to have the return string overwritten, or at least, in my bugged case when I replace `Form` by `strformat`, the strings are no longer overwritten. Is it because the return string is declared differently in these two cases?

Thank you very much,  
Hoa.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 28, 2023, 12:00pm UTC](https://root-forum.cern.ch/t/question-about-the-function-form-that-return-a-format-string/53815/2 "2023-02-28T12:00:32Z")

</div>

I think @Axel can give more details about this

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 28, 2023, 1:40pm UTC](https://root-forum.cern.ch/t/question-about-the-function-form-that-return-a-format-string/53815/3 "2023-02-28T13:40:30Z")

</div>

> From my understanding, the function uses `vsnprintf`

It is not about the `vsnprintf` but about what it is writing. The function `strformat` in your post is safe but requires the user to `free` the returned buffer (or incur a memory leak). The `Form` free function on the other hand is closer to the following tweak (with the buffer over-run actually handled) of `strformat`:

```auto
static char* return_string = (char*)malloc( required_length * sizeof(char) );

```

So it does not requires the user/caller to delete the buffer but is also always writting in the same (thread local) buffer.

The better solution is usually to use:

```auto
TString str;
str.Form(...);

```

which uses the 'TString` created before hand as the buffer and thus does not risk overwritting.

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [March 1, 2023, 5:57am UTC](https://root-forum.cern.ch/t/question-about-the-function-form-that-return-a-format-string/53815/4 "2023-03-01T05:57:27Z")

</div>

Thank you very much, I understand it now.
