Some code that compiles but shouldn't

uranuz neuranuz at gmail.com
Mon Dec 30 18:18:49 UTC 2019


I have created library/ framework to handle JSON-RPC requests 
using D methods. I use some *template magic* to translate 
JSON-RPC parameters and return values from/ and to JSON. And I 
have encountered funny bug that at first was hard to find. My 
programme just segfaulted when call to this method occured:

	void getCompiledTemplate(HTTPContext ctx)
	{
		import std.exception: enforce;
		enforce(ctx, `ctx is null`);
		enforce(ctx.request, `ctx.request is null`);
		enforce(ctx.request.form, `ctx.request is null`);
		enforce(ivyEngine !is null, `ivyEngine is null`);
		string moduleName = ctx.request.form[`moduleName`];
		auto mod = ivyEngine.getByModuleName(moduleName);
		return ctx.response.write(mod.toStdJSON().toString());
	}


So as you see I have added a lot of enforce to test if all 
variables are not null. But nothing was null and the reason of 
segfault were unclear.
Today I just went home. Opened a bottle of beer. And have noticed 
that function is marked as returning `void`, but in fact it 
doesn't. When I fixed this segfault have gone. But why this even 
compiled?! Interesting...


More information about the Digitalmars-d-learn mailing list