Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Deferenced variables revealed by CodeQL #200

Merged
merged 1 commit into from
Feb 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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