File 707e3265-Fix-file-content-malloc-to-avoid-reading-beyond-buff.patch of Package libsass.7438

From 707e326525117cba20211d39b0a936f0a95c1094 Mon Sep 17 00:00:00 2001
From: Marcel Greter <marcel.greter@ocbnet.ch>
Date: Wed, 12 Jul 2017 01:30:49 +0200
Subject: [PATCH] Fix file content malloc to avoid reading beyond buffer

Add another byte to avoid lexer going into unallocated
memory land. End of file handling was always pretty poor
with libsass. This is just another hack and no real fix.
---
 src/file.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Index: libsass-3.3.2/src/file.cpp
===================================================================
--- libsass-3.3.2.orig/src/file.cpp
+++ libsass-3.3.2/src/file.cpp
@@ -367,9 +367,11 @@ namespace Sass {
         DWORD dwFileLength = GetFileSize(hFile, NULL);
         if (dwFileLength == INVALID_FILE_SIZE) return 0;
         // allocate an extra byte for the null char
-        pBuffer = (BYTE*)malloc((dwFileLength+1)*sizeof(BYTE));
+        // and another one for edge-cases in lexer
+        pBuffer = (BYTE*)malloc((dwFileLength+2)*sizeof(BYTE));
         ReadFile(hFile, pBuffer, dwFileLength, &dwBytes, NULL);
-        pBuffer[dwFileLength] = '\0';
+        pBuffer[dwFileLength+0] = '\0';
+        pBuffer[dwFileLength+1] = '\0';
         CloseHandle(hFile);
         // just convert from unsigned char*
         char* contents = (char*) pBuffer;
@@ -381,10 +383,12 @@ namespace Sass {
         if (file.is_open()) {
           size_t size = file.tellg();
           // allocate an extra byte for the null char
-          contents = (char*) malloc((size+1)*sizeof(char));
+          // and another one for edge-cases in lexer
+          contents = (char*) malloc((size+2)*sizeof(char));
           file.seekg(0, std::ios::beg);
           file.read(contents, size);
-          contents[size] = '\0';
+          contents[size+0] = '\0';
+          contents[size+1] = '\0';
           file.close();
         }
       #endif
openSUSE Build Service is sponsored by