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

Guard against NullReference while creating HostContext #2343

Merged

Conversation

nikola-jokic
Copy link
Contributor

@nikola-jokic nikola-jokic commented Dec 27, 2022

Fixes null reference exception creating HostContext if the .runner file exists, but is empty.
Null reference exception is described in issue #2342, but this does not solve the issue itself.

It just lets the HostContext proceed and the error message will be displayed as the runner is already configured.

@nikola-jokic nikola-jokic requested a review from a team as a code owner December 27, 2022 12:43
AvaStancu
AvaStancu previously approved these changes Jan 16, 2023
@@ -221,8 +221,11 @@ public HostContext(string hostType, string logFile = null)
if (File.Exists(runnerFile))
{
var runnerSettings = IOUtil.LoadObject<RunnerSettings>(runnerFile);
_userAgents.Add(new ProductInfoHeaderValue("RunnerId", runnerSettings.AgentId.ToString(CultureInfo.InvariantCulture)));
_userAgents.Add(new ProductInfoHeaderValue("GroupId", runnerSettings.PoolId.ToString(CultureInfo.InvariantCulture)));
if (runnerSettings != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just throw and crash the runner if runnerSettings is NULL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is going to say that the runner is unauthenticated. This occurs when the file is empty, so json is invalid.
The intention is not to throw null reference here and leave it up to the client saying that the runner is not authenticated.
If you think it is better to provide a different message or throw other exception, I can work on that ☺️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the assert under IOUtil.LoadObject? ex: IOUtil.LoadObject(string file, bool required = false/true)
and the IOUtil.LoadObject can throw if it gets an empty file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

string json = File.ReadAllText(path, Encoding.UTF8);
if (string.IsNullOrEmpty(json))
{
throw new InvalidOperationException($"File {path} is empty");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TingluoHuang any suggestions for exception that we should throw ☺️ ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to merge public static T LoadObject<T>(string path, bool required) merge with public static T LoadObject<T>(string path) to be something like public static T LoadObject<T>(string path, bool required = false)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArgumentNullException or ArgumentException both seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied ArgumentNullException if file is empty or null, ArgumentException if convert from json returns null

TingluoHuang
TingluoHuang previously approved these changes Mar 29, 2023
TingluoHuang
TingluoHuang previously approved these changes Mar 29, 2023
@nikola-jokic nikola-jokic merged commit 1ceb1a6 into actions:main Mar 30, 2023
@nikola-jokic nikola-jokic deleted the nikola-jokic/host-context-null-guard branch March 30, 2023 13:39
olira pushed a commit to olira/runner that referenced this pull request Apr 5, 2023
* Guard against NullReference while creating HostContext

* Throw on IOUtil loading object if content is empty or object is null

* removed unused dependency

* Changed exceptions to ArgumentNullException and ArgumentException

* fixed my mistake on LoadObject

* fixed tests

* trigger workflows

* trigger workflows

* encode files using utf8
nikola-jokic added a commit to nikola-jokic/runner that referenced this pull request May 12, 2023
* Guard against NullReference while creating HostContext

* Throw on IOUtil loading object if content is empty or object is null

* removed unused dependency

* Changed exceptions to ArgumentNullException and ArgumentException

* fixed my mistake on LoadObject

* fixed tests

* trigger workflows

* trigger workflows

* encode files using utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants