ChallengeFind the bug

time to read 1 min | 137 words

Here is another subtle bug:

public class Log
	string path;

	public Log(string path)

	public TextWriter GetLogger(string name)
		string fileName = Path.Combine(path, name);
			return File.AppendText(fileName);
		return File.CreateText(fileName);
  • Assume that the name that we pass to GetLogger() is always valid.
  • Assume that there are no permission errors.
  • Assume that no one is going to delete the log directory behind our back.
  • Assume no file system level errors (like file system full, etc).

Update: A few readers pointed out a few more problems with the code, so let us constrain it even further...

  • Assume single threaded access.
  • Assume proper disposable of file handle.

Under what conditions will this fail?

More posts in "Challenge" series:

  1. (03 Jan 2020) Spot the bug in the stream–answer
  2. (15 Feb 2010) Where is the optimization?