@@ -222,21 +222,20 @@ OpFoldResult FormatBinOp::fold(FoldAdaptor adaptor) {
222222OpFoldResult FormatScientificOp::fold (FoldAdaptor adaptor) {
223223
224224 if (auto floatAttr = llvm::dyn_cast_or_null<FloatAttr>(adaptor.getValue ())) {
225- int bufferSize = 2 + adaptor.getFracDigits ();
226225 std::string widthString = getIsLeftAligned () ? " -" : " " ;
227226 if (adaptor.getFieldWidth ().has_value ()) {
228- widthString = std::to_string (adaptor.getFieldWidth ().value ());
229- bufferSize += adaptor.getFieldWidth ().value ();
227+ widthString += std::to_string (adaptor.getFieldWidth ().value ());
230228 }
231229 std::string fmtSpecifier =
232230 " %" + widthString + " ." + std::to_string (adaptor.getFracDigits ()) + " e" ;
233- char *floatFmtBuffer = (char *)malloc (bufferSize);
234231
235- while (snprintf (floatFmtBuffer, bufferSize, fmtSpecifier.c_str (),
236- floatAttr.getValue ().convertToDouble ()) >= bufferSize) {
237- bufferSize *= 2 ;
238- floatFmtBuffer = (char *)realloc (floatFmtBuffer, bufferSize);
239- }
232+ // Calculates number of bytes needed to store the format string
233+ // excluding the null terminator
234+ int bufferSize = std::snprintf (nullptr , 0 , fmtSpecifier.c_str (),
235+ floatAttr.getValue ().convertToDouble ());
236+ std::string floatFmtBuffer (bufferSize, ' \0 ' );
237+ snprintf (floatFmtBuffer.data (), bufferSize + 1 , fmtSpecifier.c_str (),
238+ floatAttr.getValue ().convertToDouble ());
240239 return StringAttr::get (getContext (), floatFmtBuffer);
241240 }
242241 return {};
@@ -245,21 +244,20 @@ OpFoldResult FormatScientificOp::fold(FoldAdaptor adaptor) {
245244OpFoldResult FormatFloatOp::fold (FoldAdaptor adaptor) {
246245
247246 if (auto floatAttr = llvm::dyn_cast_or_null<FloatAttr>(adaptor.getValue ())) {
248- int bufferSize = 2 + adaptor.getFracDigits ();
249247 std::string widthString = getIsLeftAligned () ? " -" : " " ;
250248 if (adaptor.getFieldWidth ().has_value ()) {
251- widthString = std::to_string (adaptor.getFieldWidth ().value ());
252- bufferSize += adaptor.getFieldWidth ().value ();
249+ widthString += std::to_string (adaptor.getFieldWidth ().value ());
253250 }
254251 std::string fmtSpecifier =
255252 " %" + widthString + " ." + std::to_string (adaptor.getFracDigits ()) + " f" ;
256- char *floatFmtBuffer = (char *)malloc (bufferSize);
257253
258- while (snprintf (floatFmtBuffer, bufferSize, fmtSpecifier.c_str (),
259- floatAttr.getValue ().convertToDouble ()) >= bufferSize) {
260- bufferSize *= 2 ;
261- floatFmtBuffer = (char *)realloc (floatFmtBuffer, bufferSize);
262- }
254+ // Calculates number of bytes needed to store the format string
255+ // excluding the null terminator
256+ int bufferSize = std::snprintf (nullptr , 0 , fmtSpecifier.c_str (),
257+ floatAttr.getValue ().convertToDouble ());
258+ std::string floatFmtBuffer (bufferSize, ' \0 ' );
259+ snprintf (floatFmtBuffer.data (), bufferSize + 1 , fmtSpecifier.c_str (),
260+ floatAttr.getValue ().convertToDouble ());
263261 return StringAttr::get (getContext (), floatFmtBuffer);
264262 }
265263 return {};
@@ -268,21 +266,20 @@ OpFoldResult FormatFloatOp::fold(FoldAdaptor adaptor) {
268266OpFoldResult FormatGeneralOp::fold (FoldAdaptor adaptor) {
269267
270268 if (auto floatAttr = llvm::dyn_cast_or_null<FloatAttr>(adaptor.getValue ())) {
271- int bufferSize = 2 + adaptor.getFracDigits ();
272269 std::string widthString = getIsLeftAligned () ? " -" : " " ;
273270 if (adaptor.getFieldWidth ().has_value ()) {
274- widthString = std::to_string (adaptor.getFieldWidth ().value ());
275- bufferSize += adaptor.getFieldWidth ().value ();
271+ widthString += std::to_string (adaptor.getFieldWidth ().value ());
276272 }
277273 std::string fmtSpecifier =
278274 " %" + widthString + " ." + std::to_string (adaptor.getFracDigits ()) + " g" ;
279- char *floatFmtBuffer = (char *)malloc (bufferSize);
280275
281- while (snprintf (floatFmtBuffer, bufferSize, fmtSpecifier.c_str (),
282- floatAttr.getValue ().convertToDouble ()) >= bufferSize) {
283- bufferSize *= 2 ;
284- floatFmtBuffer = (char *)realloc (floatFmtBuffer, bufferSize);
285- }
276+ // Calculates number of bytes needed to store the format string
277+ // excluding the null terminator
278+ int bufferSize = std::snprintf (nullptr , 0 , fmtSpecifier.c_str (),
279+ floatAttr.getValue ().convertToDouble ());
280+ std::string floatFmtBuffer (bufferSize, ' \0 ' );
281+ snprintf (floatFmtBuffer.data (), bufferSize + 1 , fmtSpecifier.c_str (),
282+ floatAttr.getValue ().convertToDouble ());
286283 return StringAttr::get (getContext (), floatFmtBuffer);
287284 }
288285 return {};
0 commit comments