Skip to content

Commit

Permalink
Fix Deferenced variables revealed by CodeQL
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Feb 5, 2022
1 parent a23d083 commit 4476e58
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/AzureIoTHub.Portal/Server/Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace AzureIoTHub.Portal.Server.Helpers
{
using System;
using System.Collections.Generic;
using AzureIoTHub.Portal.Shared.Models;
using Microsoft.Azure.Devices;
Expand Down Expand Up @@ -73,7 +74,7 @@ public static GatewayModule CreateGatewayModule(Configuration config, KeyValuePa
/// <returns>A dictionnary containing the settings and their corresponding values.</returns>
private static Dictionary<string, string> GetModuleIdentityTwinSettings(Configuration config, KeyValuePair<string, JToken> module)
{
Dictionary<string, string> twinSettings = new ();
var twinSettings = new Dictionary<string, string>();

if (config.Content.ModulesContent != null)
{
Expand All @@ -99,23 +100,25 @@ private static Dictionary<string, string> GetModuleIdentityTwinSettings(Configur
/// <returns>A dictionnary containing the environment variables and their corresponding values.</returns>
private static Dictionary<string, string> GetEnvironmentVariables(KeyValuePair<string, JToken> module)
{
Dictionary<string, string> envVariables = new ();
var envVariables = new Dictionary<string, string>();

// Converts the object to a JObject to access its properties more easily
JObject moduleProperties = module.Value as JObject;

if (moduleProperties == null)
{
throw new InvalidOperationException("Unable to parse module environment variables!");
}

// Only exists if the module contains environment variables
if (moduleProperties.ContainsKey("env"))
if (!moduleProperties.ContainsKey("env"))
{
foreach (JProperty val in moduleProperties["env"])
{
var variableName = val.Name;
return envVariables;
}

// Converts the object to a JObject to access its properties more easily
JObject tmp = val.Value as JObject;
var variableValue = tmp["value"];
envVariables.Add(variableName, variableValue.ToString());
}
foreach (JProperty val in moduleProperties["env"])
{
envVariables.Add(val.Name, val["value"]?.ToString());
}

return envVariables;
Expand Down

0 comments on commit 4476e58

Please sign in to comment.