Skip to content

Commit f26b3d2

Browse files
author
FARHAD SHAKERIN
authored
[FR DateTimeV2] Stack overflow fix in .NET (#3050)
1 parent c99de05 commit f26b3d2

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDurationParser.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,16 @@ private DateTimeResolutionResult ParseMergedDuration(string text, DateObject ref
398398
var durationExtractor = this.config.DurationExtractor;
399399

400400
// DurationExtractor without parameter will not extract merged duration
401-
var ers = durationExtractor.Extract(text, referenceTime);
401+
402+
// TrimStart() was added to address a bug with french duration expression "depuis ans"
403+
// for which the basecase of the recursive call (i.e., if(ers.Count <= 1))
404+
// would never be reached in which case the stack would overflow.
405+
// The statement if(minStart){...} is meant to find the isolated unit as explained in
406+
// the below comment. However, if there is whitespace before the extacted entity
407+
// (as in " ans") the minStart will be greater than 1 and the Followed Unit regex
408+
// keeps on matching with "ans" and it adds it to ers and it always has
409+
// more than one item in it, hence the recursion never ends.
410+
var ers = durationExtractor.Extract(text.TrimStart(), referenceTime);
402411

403412
// If the duration extractions do not start at 0, check if the input starts with an isolated unit.
404413
// This happens for example with patterns like "next week and 3 days" where "next" is not part of the extraction.

Specs/DateTime/French/DateTimeModel.json

+24-3
Original file line numberDiff line numberDiff line change
@@ -3624,7 +3624,7 @@
36243624
"End": 67
36253625
}
36263626
]
3627-
},
3627+
},
36283628
{
36293629
"Input": "Je vais rentrer le 4 janvier 2019.",
36303630
"NotSupported": "dotnet, javascript, python, java",
@@ -8694,6 +8694,27 @@
86948694
}
86958695
]
86968696
},
8697+
{
8698+
"Input": "bonjour la maison n'est plus habitée depuis ans",
8699+
"Results": [
8700+
{
8701+
"Text": "depuis ans",
8702+
"Start": 37,
8703+
"End": 46,
8704+
"TypeName": "datetimeV2.duration",
8705+
"Resolution": {
8706+
"values": [
8707+
{
8708+
"timex": "P1Y",
8709+
"Mod": "since",
8710+
"type": "duration",
8711+
"value": "31536000"
8712+
}
8713+
]
8714+
}
8715+
}
8716+
]
8717+
},
86978718
{
86988719
"Input": "Rechercher les enregistrements durent moins de 2 heures ou plus de 4 jours et pas moins de 30 minutes.",
86998720
"NotSupported": "dotnet, javascript, python, java",
@@ -21372,7 +21393,7 @@
2137221393
"type": "time",
2137321394
"value": "08:00:00"
2137421395
},
21375-
{
21396+
{
2137621397
"timex": "T20",
2137721398
"type": "time",
2137821399
"value": "20:00:00"
@@ -21400,7 +21421,7 @@
2140021421
"type": "time",
2140121422
"value": "01:00:00"
2140221423
},
21403-
{
21424+
{
2140421425
"timex": "T13",
2140521426
"type": "time",
2140621427
"value": "13:00:00"

0 commit comments

Comments
 (0)