diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 577d857..afbdbb5 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -188,6 +188,11 @@
 const DRUPAL_KILOBYTE = 1024;
 
 /**
+ * The maximum number of characters in a module or theme name.
+ */
+const DRUPAL_MODULE_NAME_MAX_LENGTH = 40;
+
+/**
  * Special system language code (only applicable to UI language).
  *
  * Refers to the language used in Drupal and module/theme source code. Drupal
diff --git a/core/includes/module.inc b/core/includes/module.inc
index f6d47b6..4984d6a 100644
--- a/core/includes/module.inc
+++ b/core/includes/module.inc
@@ -465,6 +465,13 @@ function module_enable($module_list, $enable_dependencies = TRUE) {
         unset($module_list[$module]);
         continue;
       }
+      // Throw an exception if the module name is too long.
+      if (strlen($module) > DRUPAL_MODULE_NAME_MAX_LENGTH) {
+        throw new \Exception(format_string('Module name %name is over the maximum allowed length of @max characters.', array(
+          '%name' => $module,
+          '@max' => DRUPAL_MODULE_NAME_MAX_LENGTH,
+        )));
+      }
       $module_list[$module] = $module_data[$module]->sort;
 
       // Add dependencies to the list, with a placeholder weight.
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index bab2af5..cff98ee 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -1515,6 +1515,14 @@ function theme_enable($theme_list) {
   $theme_config = config('system.theme');
   $disabled_themes = config('system.theme.disabled');
   foreach ($theme_list as $key) {
+    // Throw an exception if the theme name is too long.
+    if (strlen($key) > DRUPAL_MODULE_NAME_MAX_LENGTH) {
+      throw new \Exception(format_string('Theme name %name is over the maximum allowed length of @max characters.', array(
+        '%name' => $key,
+        '@max' => DRUPAL_MODULE_NAME_MAX_LENGTH,
+      )));
+    }
+
     // The value is not used; the weight is ignored for themes currently.
     $theme_config->set("enabled.$key", 0);
     $disabled_themes->clear($key);
diff --git a/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php b/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php
index b2d08fc..1b41d3e 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php
@@ -44,4 +44,19 @@ function testRequiredModuleSchemaVersions() {
     $this->assertTrue($version > 0, 'User module version is > 0.');
   }
 
+  /**
+   * Tests that an exception is thrown when a module name is too long.
+   */
+  function testModuleNameLength() {
+    $module_name = 'invalid_module_name_over_the_maximum_allowed_character_length';
+    $message = format_string('Exception thrown when enabling module %name with a name length over the allowed maximum', array('%name' => $module_name));
+    try {
+      module_enable(array($module_name));
+      $this->fail($message);
+    }
+    catch (\Exception $e) {
+      $this->pass($message);
+    }
+  }
+
 }
diff --git a/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.info b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.info
new file mode 100644
index 0000000..24c3a2a
--- /dev/null
+++ b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.info
@@ -0,0 +1,6 @@
+name = "Module name length test"
+description = "Test module with a name over the maximum allowed characters."
+package = Testing
+version = VERSION
+core = 8.x
+hidden = TRUE
diff --git a/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.module b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.module
new file mode 100644
index 0000000..7967447
--- /dev/null
+++ b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.module
@@ -0,0 +1,8 @@
+<?php
+
+/**
+ * @file
+ * Module with a module name over the maximum allowed number of characters.
+ *
+ * @see DRUPAL_MODULE_NAME_MAX_LENGTH
+ */
