diff --git a/.appveyor/install.bat b/.appveyor/install.bat
index 8125dc79c..f9f53a73d 100644
--- a/.appveyor/install.bat
+++ b/.appveyor/install.bat
@@ -28,6 +28,9 @@ GOTO:EOF
     git clone -q --branch=master https://github.com/telegramdesktop/dependencies_windows.git %LIB_DIR%
     cd %LIB_DIR%
 
+    call:logInfo "Clone GSL"
+    git clone https://github.com/Microsoft/GSL.git
+
     call prepare.bat
 GOTO:EOF
 
diff --git a/.travis/build.sh b/.travis/build.sh
index baa43cb27..f6e97b10d 100755
--- a/.travis/build.sh
+++ b/.travis/build.sh
@@ -81,6 +81,9 @@ build() {
   # Patched GYP (supports cmake precompiled headers)
   getGYP
 
+  # Guideline Support Library
+  getGSL
+
   # Configure the build
   if [[ $BUILD_VERSION == *"disable_autoupdate"* ]]; then
     GYP_DEFINES+=",TDESKTOP_DISABLE_AUTOUPDATE"
@@ -539,6 +542,11 @@ buildCustomQt() {
   sudo make install
 }
 
+getGSL() {
+  cd "$EXTERNAL"
+  git clone https://github.com/Microsoft/GSL.git
+}
+
 getGYP() {
   travisStartFold "Getting patched GYP"
 
@@ -594,6 +602,7 @@ buildTelegram() {
   cd "$UPSTREAM/Telegram/gyp"
   "$GYP_PATH/gyp" \
       -Dbuild_defines=${GYP_DEFINES:1} \
+      -Dlinux_path_gsl=$EXTERNAL/GSL \
       -Dlinux_path_xkbcommon=$XKB_PATH \
       -Dlinux_path_va=$VA_PATH \
       -Dlinux_path_vdpau=$VDPAU_PATH \
diff --git a/README.md b/README.md
index 02cef5a25..6d1803920 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,7 @@ The source code is published under GPLv3 with OpenSSL exception, the license is
 * OpenAL Soft ([LGPL](http://kcat.strangesoft.net/openal.html))
 * Opus codec ([BSD license](http://www.opus-codec.org/license/))
 * FFmpeg ([LGPL](https://www.ffmpeg.org/legal.html))
+* Guideline Support Library ([MIT License](https://github.com/Microsoft/GSL/blob/master/LICENSE))
 * Open Sans font ([Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0.html))
 
 ## Build instructions
diff --git a/Telegram/SourceFiles/core/utils.h b/Telegram/SourceFiles/core/utils.h
index 075c00370..ed05b702a 100644
--- a/Telegram/SourceFiles/core/utils.h
+++ b/Telegram/SourceFiles/core/utils.h
@@ -24,6 +24,34 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
 #include <array>
 #include <algorithm>
 #include <set>
+#include <gsl/gsl>
+
+// Release build assertions.
+inline void t_noop() {
+}
+inline void t_assert_fail(const char *message, const char *file, int32 line) {
+	auto info = qsl("%1 %2:%3").arg(message).arg(file).arg(line);
+	LOG(("Assertion Failed! ") + info);
+	SignalHandlers::setCrashAnnotation("Assertion", info);
+
+	volatile int *t_assert_nullptr = nullptr;
+	*t_assert_nullptr = 0;
+}
+#define t_assert_full(condition, message, file, line) ((GSL_UNLIKELY(!(condition))) ? t_assert_fail(message, file, line) : t_noop())
+#define t_assert_c(condition, comment) t_assert_full(condition, "\"" #condition "\" (" comment ")", __FILE__, __LINE__)
+#define t_assert(condition) t_assert_full(condition, "\"" #condition "\"", __FILE__, __LINE__)
+
+// Declare our own versions of Expects() and Ensures().
+// Let them crash with reports and logging.
+#ifdef Expects
+#undef Expects
+#define Expects(condition) t_assert_full(condition, "\"" #condition "\"", __FILE__, __LINE__)
+#endif // Expects
+
+#ifdef Ensures
+#undef Ensures
+#define Ensures(condition) t_assert_full(condition, "\"" #condition "\"", __FILE__, __LINE__)
+#endif // Ensures
 
 namespace base {
 
@@ -133,6 +161,14 @@ using set_of_unique_ptr = std::set<std::unique_ptr<T>, base::pointer_comparator<
 template <typename T>
 using set_of_shared_ptr = std::set<std::shared_ptr<T>, base::pointer_comparator<T>>;
 
+using byte_span = gsl::span<gsl::byte>;
+using const_byte_span = gsl::span<const gsl::byte>;
+
+inline void copy_bytes(byte_span destination, const_byte_span source) {
+	Expects(destination.size() >= source.size());
+	memcpy(destination.data(), source.data(), source.size());
+}
+
 } // namespace base
 
 // using for_const instead of plain range-based for loop to ensure usage of const_iterator
@@ -192,18 +228,6 @@ inline void accumulate_max(T &a, const T &b) { if (a < b) a = b; }
 template <typename T>
 inline void accumulate_min(T &a, const T &b) { if (a > b) a = b; }
 
-static volatile int *t_assert_nullptr = nullptr;
-inline void t_noop() {}
-inline void t_assert_fail(const char *message, const char *file, int32 line) {
-	QString info(qsl("%1 %2:%3").arg(message).arg(file).arg(line));
-	LOG(("Assertion Failed! %1 %2:%3").arg(info));
-	SignalHandlers::setCrashAnnotation("Assertion", info);
-	*t_assert_nullptr = 0;
-}
-#define t_assert_full(condition, message, file, line) ((!(condition)) ? t_assert_fail(message, file, line) : t_noop())
-#define t_assert_c(condition, comment) t_assert_full(condition, "\"" #condition "\" (" comment ")", __FILE__, __LINE__)
-#define t_assert(condition) t_assert_full(condition, "\"" #condition "\"", __FILE__, __LINE__)
-
 class Exception : public std::exception {
 public:
 	Exception(const QString &msg, bool isFatal = true) : _fatal(isFatal), _msg(msg.toUtf8()) {
diff --git a/Telegram/SourceFiles/logs.cpp b/Telegram/SourceFiles/logs.cpp
index 17fd4b685..d1ce63efa 100644
--- a/Telegram/SourceFiles/logs.cpp
+++ b/Telegram/SourceFiles/logs.cpp
@@ -748,15 +748,13 @@ namespace internal {
 	};
 	std::unique_ptr<SomeAllocatedMemoryChunk> SomeAllocatedMemory;
 
-	void OperatorNewHandler() {
-		std::set_new_handler(nullptr);
-		SomeAllocatedMemory.reset();
-		t_assert(!"Could not allocate!");
-	}
-
 	void InstallOperatorNewHandler() {
 		SomeAllocatedMemory = std::make_unique<SomeAllocatedMemoryChunk>();
-		std::set_new_handler(OperatorNewHandler);
+		std::set_new_handler([] {
+			std::set_new_handler(nullptr);
+			SomeAllocatedMemory.reset();
+			t_assert(!"Could not allocate!");
+		});
 	}
 
 	Qt::HANDLE ReportingThreadId = nullptr;
diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp
index 25be59374..dc308ba0e 100644
--- a/Telegram/SourceFiles/mainwidget.cpp
+++ b/Telegram/SourceFiles/mainwidget.cpp
@@ -4481,7 +4481,7 @@ DataIsLoadedResult allDataLoadedForMessage(const MTPMessage &msg) {
 		}
 		switch (d.vaction.type()) {
 		case mtpc_messageActionChatAddUser: {
-			for_const(const MTPint &userId, d.vaction.c_messageActionChatAddUser().vusers.c_vector().v) {
+			for_const (const MTPint &userId, d.vaction.c_messageActionChatAddUser().vusers.c_vector().v) {
 				if (!App::userLoaded(peerFromUser(userId))) {
 					return DataIsLoadedResult::NotLoaded;
 				}
diff --git a/Telegram/gyp/Telegram.gyp b/Telegram/gyp/Telegram.gyp
index fc52ff52c..3466d32be 100644
--- a/Telegram/gyp/Telegram.gyp
+++ b/Telegram/gyp/Telegram.gyp
@@ -94,6 +94,7 @@
       '<(libs_loc)/openal-soft/include',
       '<(minizip_loc)',
       '<(sp_media_key_tap_loc)',
+      '<(libs_loc)/GSL/include',
     ],
     'sources': [
       '<@(qrc_files)',
diff --git a/Telegram/gyp/telegram_linux.gypi b/Telegram/gyp/telegram_linux.gypi
index b42e7443f..dd17ac26d 100644
--- a/Telegram/gyp/telegram_linux.gypi
+++ b/Telegram/gyp/telegram_linux.gypi
@@ -33,12 +33,14 @@
       'linux_path_va%': '/usr/local',
       'linux_path_vdpau%': '/usr/local',
       'linux_path_breakpad%': '<(libs_loc)/breakpad',
+      'linux_path_gsl%': '<(libs_loc)/GSL',
     },
     'include_dirs': [
       '/usr/local/include',
       '<(linux_path_ffmpeg)/include',
       '<(linux_path_openal)/include',
       '<(linux_path_breakpad)/include/breakpad',
+      '<(linux_path_gsl)/include',
     ],
     'library_dirs': [
       '/usr/local/lib',
diff --git a/doc/building-cmake.md b/doc/building-cmake.md
index a8b9e79ef..5bbe35f10 100644
--- a/doc/building-cmake.md
+++ b/doc/building-cmake.md
@@ -29,6 +29,10 @@ Install dev libraries
 
     sudo apt-get install libexif-dev liblzma-dev libz-dev libssl-dev libappindicator-dev libunity-dev libicu-dev libdee-dev
 
+From **/home/user/TBuild/Libraries** run
+
+    git clone https://github.com/Microsoft/GSL.git
+
 ####zlib 1.2.8
 
 http://www.zlib.net/ > Download [**zlib source code, version 1.2.8, zipfile format**](http://zlib.net/zlib128.zip)
diff --git a/doc/building-msvc.md b/doc/building-msvc.md
index 2422b677a..ab29022a9 100644
--- a/doc/building-msvc.md
+++ b/doc/building-msvc.md
@@ -51,6 +51,10 @@ and run
 
 ## Prepare libraries
 
+From **D:\\TBuild\\Libraries** run
+
+    git clone https://github.com/Microsoft/GSL.git
+
 ### OpenSSL
 
 Go to **D:\\TBuild\\Libraries** and run
diff --git a/doc/building-qtcreator.md b/doc/building-qtcreator.md
index da96bf138..0c27ad5a8 100644
--- a/doc/building-qtcreator.md
+++ b/doc/building-qtcreator.md
@@ -32,6 +32,10 @@ Install dev libraries
 
     sudo apt-get install libexif-dev liblzma-dev libz-dev libssl-dev libappindicator-dev libunity-dev
 
+From **/home/user/TBuild/Libraries** run
+
+    git clone https://github.com/Microsoft/GSL.git
+
 ####zlib 1.2.8
 
 http://www.zlib.net/ > Download [**zlib source code, version 1.2.8, zipfile format**](http://zlib.net/zlib128.zip)
diff --git a/doc/building-xcode-old.md b/doc/building-xcode-old.md
index c779f835d..1ff67df0b 100644
--- a/doc/building-xcode-old.md
+++ b/doc/building-xcode-old.md
@@ -32,6 +32,10 @@ In your build Terminal run
 
 to set minimal supported OS version to 10.6 for future console builds.
 
+From **/Users/user/TBuild/Libraries** run
+
+    git clone https://github.com/Microsoft/GSL.git
+
 ####custom build of libc++
 
 From **/Users/user/TBuild/Libraries/macold** run
diff --git a/doc/building-xcode.md b/doc/building-xcode.md
index 14209c8a9..c68f82367 100644
--- a/doc/building-xcode.md
+++ b/doc/building-xcode.md
@@ -22,6 +22,10 @@ In your build Terminal run:
 
 to set minimal supported OS version to 10.8 for future console builds.
 
+From **/Users/user/TBuild/Libraries** run
+
+    git clone https://github.com/Microsoft/GSL.git
+
 ####zlib 1.2.8
 
 http://www.zlib.net/ > Download [**zlib source code, version 1.2.8**](http://www.zlib.net/fossils/zlib-1.2.8.tar.gz)