-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathflutter_angle_crash_fix.patch
More file actions
159 lines (152 loc) · 13.2 KB
/
flutter_angle_crash_fix.patch
File metadata and controls
159 lines (152 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
diff --git a/flutter_angle/windows/flutter_angle_plugin.cpp b/flutter_angle/windows/flutter_angle_plugin.cpp
index c703f56..2156630 100755
--- a/flutter_angle/windows/flutter_angle_plugin.cpp
+++ b/flutter_angle/windows/flutter_angle_plugin.cpp
@@ -127,6 +127,22 @@ namespace {
return;
}
+ // Input validation to prevent crashes
+ const int MAX_TEXTURE_SIZE = 8192;
+ if (width <= 0 || height <= 0) {
+ result->Error("Invalid texture dimensions", "Width and height must be positive");
+ return;
+ }
+ if (width > MAX_TEXTURE_SIZE || height > MAX_TEXTURE_SIZE) {
+ result->Error("Texture too large", "Width and height must be less than " + std::to_string(MAX_TEXTURE_SIZE));
+ return;
+ }
+ // Check for potential integer overflow
+ if (width > INT_MAX / height / 4) {
+ result->Error("Texture too large", "Texture dimensions would cause integer overflow");
+ return;
+ }
+
std::unique_ptr<FlutterGLTexture> flutterGLTexture;
try{
@@ -147,8 +163,14 @@ namespace {
renderers.insert(RendererMap::value_type(textureId, std::move(flutterGLTexture)));
renderers[textureId]->createTexture(result);
}
- catch (OpenGLException ex){
- result->Error(ex.message + ':' + std::to_string(ex.error));
+ catch (const OpenGLException& ex){
+ result->Error(std::string(ex.message) + ":" + std::to_string(ex.error));
+ }
+ catch (const std::exception& ex) {
+ result->Error("Standard exception", ex.what());
+ }
+ catch (...) {
+ result->Error("Unknown exception", "An unknown exception occurred during texture creation");
}
}
else if (method_call.method_name().compare("updateTexture") == 0 || method_call.method_name().compare("textureFrameAvailable") == 0) {
diff --git a/flutter_angle/windows/flutter_gl_texture.cpp b/flutter_angle/windows/flutter_gl_texture.cpp
index 4291f60..6d6a826 100755
--- a/flutter_angle/windows/flutter_gl_texture.cpp
+++ b/flutter_angle/windows/flutter_gl_texture.cpp
@@ -144,12 +144,47 @@ EGLInfo FlutterGLTexture::initOpenGL(std::unique_ptr<flutter::MethodResult<flutt
}
void FlutterGLTexture::changeSize(int setWidth, int setHeight, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>>& result) {
+ // Input validation to prevent buffer overrun
+ const int MAX_TEXTURE_SIZE = 8192; // Reasonable maximum texture size
+ if (setWidth <= 0 || setHeight <= 0) {
+ result->Error("Invalid texture dimensions", "Width and height must be positive");
+ return;
+ }
+ if (setWidth > MAX_TEXTURE_SIZE || setHeight > MAX_TEXTURE_SIZE) {
+ result->Error("Texture too large", "Width and height must be less than " + std::to_string(MAX_TEXTURE_SIZE));
+ return;
+ }
+
+ // Check for integer overflow before multiplication
+ if (setWidth > INT_MAX / setHeight / 4) {
+ result->Error("Texture too large", "Texture dimensions would cause integer overflow");
+ return;
+ }
+
if (setWidth == structure.width && setHeight == structure.height && didStart) {
return;
}
if(structure.useBuffer){
int64_t size = setWidth * setHeight * 4;
- pixels.reset(new uint8_t[size]);
+
+ // Additional safety check
+ if (size <= 0 || size > SIZE_MAX) {
+ result->Error("Invalid buffer size", "Calculated buffer size is invalid");
+ return;
+ }
+
+ try {
+ pixels.reset(new uint8_t[size]);
+ } catch (const std::bad_alloc& e) {
+ result->Error("Memory allocation failed", "Failed to allocate memory for texture buffer");
+ return;
+ }
+
+ // Validate pixelBuffer is initialized
+ if (!pixelBuffer) {
+ result->Error("Pixel buffer not initialized", "Pixel buffer is null");
+ return;
+ }
pixelBuffer->buffer = pixels.get();
pixelBuffer->width = setWidth;
@@ -306,14 +341,14 @@ void FlutterGLTexture::setupOpenGLResources(){
auto error = glGetError();
if (error != GL_NO_ERROR){
std::cerr << "GlError while allocating Renderbuffer" << error << std::endl;
- throw new OpenGLException("GlError while allocating Renderbuffer", error);
+ throw OpenGLException("GlError while allocating Renderbuffer", error);
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,rbo);
auto frameBufferCheck = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (frameBufferCheck != GL_FRAMEBUFFER_COMPLETE){
std::cerr << "Framebuffer error" << frameBufferCheck << std::endl;
- throw new OpenGLException("Framebuffer Error while creating Texture", frameBufferCheck);
+ throw OpenGLException("Framebuffer Error while creating Texture", frameBufferCheck);
}
error = glGetError();
@@ -349,8 +384,44 @@ void FlutterGLTexture::createTexture(std::unique_ptr<flutter::MethodResult<flutt
void FlutterGLTexture::textureFrameAvailable(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>>& result){
if(structure.useBuffer){
+ // Validate pixelBuffer and its buffer before use
+ if (!pixelBuffer) {
+ result->Error("Pixel buffer not initialized", "Pixel buffer is null");
+ return;
+ }
+ if (!pixelBuffer->buffer) {
+ result->Error("Pixel buffer data not allocated", "Pixel buffer data is null");
+ return;
+ }
+ if (pixelBuffer->width <= 0 || pixelBuffer->height <= 0) {
+ result->Error("Invalid pixel buffer dimensions", "Pixel buffer dimensions are invalid");
+ return;
+ }
+
+ // Check for potential buffer overrun
+ int64_t requiredSize = pixelBuffer->width * pixelBuffer->height * 4;
+ if (requiredSize <= 0 || requiredSize > SIZE_MAX) {
+ result->Error("Invalid buffer size for read operation", "Calculated buffer size is invalid");
+ return;
+ }
+
glBindFramebuffer(GL_FRAMEBUFFER, textures.fboId);
+
+ // Check for OpenGL errors before read operation
+ GLenum error = glGetError();
+ if (error != GL_NO_ERROR) {
+ result->Error("OpenGL error before readPixels", "OpenGL error: " + std::to_string(error));
+ return;
+ }
+
glReadPixels(0, 0, (GLsizei)pixelBuffer->width, (GLsizei)pixelBuffer->height, GL_RGBA, GL_UNSIGNED_BYTE, (void*)pixelBuffer->buffer);
+
+ // Check for OpenGL errors after read operation
+ error = glGetError();
+ if (error != GL_NO_ERROR) {
+ result->Error("OpenGL error during readPixels", "OpenGL error: " + std::to_string(error));
+ return;
+ }
}
textureRegistrar->MarkTextureFrameAvailable(textureId);
result->Success();