From 879130b7c20220545689b4b12d40a13cdf9a84a0 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 20 Jan 2012 10:56:58 -0800 Subject: [PATCH] #18364: Fixing possible stack overflows in Shape Tracing Work Item: 18364 --HG-- branch : 1.x --- .../Services/ShapeTracingFactory.cs | 14 +++++++++++++- .../Environment/WorkContextImplementation.cs | 9 ++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.DesignerTools/Services/ShapeTracingFactory.cs b/src/Orchard.Web/Modules/Orchard.DesignerTools/Services/ShapeTracingFactory.cs index 31e09dc84..730690920 100644 --- a/src/Orchard.Web/Modules/Orchard.DesignerTools/Services/ShapeTracingFactory.cs +++ b/src/Orchard.Web/Modules/Orchard.DesignerTools/Services/ShapeTracingFactory.cs @@ -21,6 +21,8 @@ namespace Orchard.DesignerTools.Services { private readonly IThemeManager _themeManager; private readonly IWebSiteFolder _webSiteFolder; private readonly IAuthorizer _authorizer; + private bool _processing; + private int _shapeId; public ShapeTracingFactory( @@ -57,6 +59,13 @@ namespace Orchard.DesignerTools.Services { return; } + // prevent reentrance as some methods could create new shapes, and trigger this event + if(_processing) { + return; + } + + _processing = true; + if (context.ShapeType != "Layout" && context.ShapeType != "DocumentZone" && context.ShapeType != "PlaceChildContent" @@ -66,16 +75,19 @@ namespace Orchard.DesignerTools.Services { && context.ShapeType != "DateTimeRelative") { var shapeMetadata = (ShapeMetadata)context.Shape.Metadata; - var currentTheme = _themeManager.GetRequestTheme(_workContext.HttpContext.Request.RequestContext); + var currentTheme = _workContext.CurrentTheme; var shapeTable = _shapeTableManager.GetShapeTable(currentTheme.Id); if (!shapeTable.Descriptors.ContainsKey(shapeMetadata.Type)) { + _processing = false; return; } shapeMetadata.Wrappers.Add("ShapeTracingWrapper"); shapeMetadata.OnDisplaying(OnDisplaying); } + + _processing = false; } public void Displaying(ShapeDisplayingContext context) {} diff --git a/src/Orchard/Environment/WorkContextImplementation.cs b/src/Orchard/Environment/WorkContextImplementation.cs index e7f0634b2..2012b7cdd 100644 --- a/src/Orchard/Environment/WorkContextImplementation.cs +++ b/src/Orchard/Environment/WorkContextImplementation.cs @@ -8,6 +8,7 @@ namespace Orchard.Environment { class WorkContextImplementation : WorkContext { readonly IComponentContext _componentContext; readonly ConcurrentDictionary> _stateResolvers = new ConcurrentDictionary>(); + readonly ConcurrentDictionary _states = new ConcurrentDictionary(); readonly IEnumerable _workContextStateProviders; public WorkContextImplementation(IComponentContext componentContext) { @@ -24,8 +25,10 @@ namespace Orchard.Environment { } public override T GetState(string name) { - var resolver = _stateResolvers.GetOrAdd(name, FindResolverForState); - return (T)resolver(); + return (T)_states.GetOrAdd(name, (x) => { + var resolver = _stateResolvers.GetOrAdd(x, FindResolverForState); + return resolver(); + }); } Func FindResolverForState(string name) { @@ -39,7 +42,7 @@ namespace Orchard.Environment { public override void SetState(string name, T value) { - _stateResolvers[name] = () => value; + _stateResolvers[name] = () => _states[name] = value; } } } \ No newline at end of file