Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 269056c

Browse files
author
Monte Goulding
committed
BooleanCreateWithBool causing a crash on ScannerAdvance
1 parent 62df16d commit 269056c

File tree

5 files changed

+98
-13
lines changed

5 files changed

+98
-13
lines changed

lcidlc/src/Interface.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ enum InterfaceError
3838
kInterfaceErrorParamAlreadyDefined,
3939
kInterfaceErrorInvalidParameterType,
4040
kInterfaceErrorOptionalParamImpliesIn,
41-
kInterfaceErrorBooleanDefaultWrongType,
41+
kInterfaceErrorNonPointerOptionalParameterMustHaveDefaultValue,
42+
kInterfaceErrorDefaultWrongType,
4243
kInterfaceErrorUnknownType,
4344
};
4445

@@ -116,8 +117,11 @@ static bool InterfaceReport(InterfaceRef self, Position p_where, InterfaceError
116117
case kInterfaceErrorOptionalParamImpliesIn:
117118
fprintf(stderr, "Optional parameters must be of 'in' type\n");
118119
break;
119-
case kInterfaceErrorBooleanDefaultWrongType:
120-
fprintf(stderr, "Boolean defaults must be either true or false\n");
120+
case kInterfaceErrorNonPointerOptionalParameterMustHaveDefaultValue:
121+
fprintf(stderr, "Delfault values must be specified for non-pointer type optional parameters\n");
122+
break;
123+
case kInterfaceErrorDefaultWrongType:
124+
fprintf(stderr, "Default specified is the wrong type\n");
121125
break;
122126
case kInterfaceErrorUnknownType:
123127
fprintf(stderr, "Unknown type '%s'\n", StringGetCStringPtr(NameGetString((NameRef)p_hint)));
@@ -402,11 +406,32 @@ bool InterfaceDefineHandlerParameter(InterfaceRef self, Position p_where, Parame
402406
// RULE: 'ref' not currently supported
403407
if (p_param_type == kParameterTypeRef)
404408
InterfaceReport(self, p_where, kInterfaceErrorInvalidParameterType, nil);
405-
406-
// RULE: optional 'boolean' default should be a name value
407-
if (NameEqualToCString(p_type, "boolean") && p_default != nil && !ValueIsName(p_default))
408-
InterfaceReport(self, p_where, kInterfaceErrorBooleanDefaultWrongType, nil);
409-
409+
410+
// RULE: only pointer types may not have a default value
411+
if (p_optional && p_default == nil &&
412+
(NameEqualToCString(p_type, "boolean") ||
413+
NameEqualToCString(p_type, "integer") ||
414+
NameEqualToCString(p_type, "real") ||
415+
NameEqualToCString(p_type, "c-data")))
416+
InterfaceReport(self, p_where, kInterfaceErrorNonPointerOptionalParameterMustHaveDefaultValue, nil);
417+
418+
// RULE: wrong default type
419+
if (p_default != nil)
420+
{
421+
bool t_correct_type = false;
422+
if (!t_correct_type)
423+
t_correct_type = NameEqualToCString(p_type, "boolean") && ValueIsBoolean(p_default);
424+
if (!t_correct_type)
425+
t_correct_type = NameEqualToCString(p_type, "integer") && ValueIsInteger(p_default);
426+
if (!t_correct_type)
427+
t_correct_type = NameEqualToCString(p_type, "real") && ValueIsReal(p_default);
428+
if (!t_correct_type)
429+
t_correct_type = (NameEqualToCString(p_type, "c-string") || NameEqualToCString(p_type, "objc-string")) && ValueIsString(p_default);
430+
431+
if (!t_correct_type)
432+
InterfaceReport(self, p_where, kInterfaceErrorDefaultWrongType, nil);
433+
}
434+
410435
// RULE: optional parameters can only be 'in'
411436
if (p_default != nil && p_param_type != kParameterTypeIn)
412437
InterfaceReport(self, p_where, kInterfaceErrorOptionalParamImpliesIn, nil);

lcidlc/src/InterfaceGenerate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ static bool InterfaceGenerateHandlers(InterfaceRef self, CoderRef p_coder)
837837
switch(t_native_type)
838838
{
839839
case kNativeTypeBoolean:
840-
CoderWriteLine(p_coder, "\t\t\tparam__%s = %s;", t_name, StringGetCStringPtr(NameGetString(t_parameter -> default_value)));
840+
CoderWriteLine(p_coder, "\t\t\tparam__%s = %s;", t_name, BooleanGetBool(t_parameter -> default_value) ? "true" : "false");
841841
break;
842842
case kNativeTypeObjcData:
843843
CoderWriteLine(p_coder, "\t\t\tsuccess = false;");

lcidlc/src/Parser.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,39 @@ static bool ParserMatchConstant(ParserRef self, ValueRef& r_value)
270270
if (!ScannerRetrieve(self -> scanner, t_token))
271271
return false;
272272

273+
bool t_is_bool = t_token -> type == kTokenTypeIdentifier;
274+
if (t_is_bool)
275+
{
276+
bool t_bool;
277+
278+
if (t_is_bool)
279+
{
280+
t_bool = !NameEqualToCString(t_token -> value, "false");
281+
t_is_bool = !t_bool;
282+
}
283+
284+
if (!t_is_bool)
285+
{
286+
t_bool = NameEqualToCString(t_token -> value, "true");
287+
t_is_bool = t_bool;
288+
}
289+
290+
if (t_is_bool)
291+
BooleanCreateWithBool(t_bool, r_value);
292+
}
293+
273294
if (!(t_token -> type == kTokenTypeString ||
274295
t_token -> type == kTokenTypeNumber ||
275-
(t_token->type == kTokenTypeIdentifier &&
276-
(NameEqualToCString(t_token -> value, "false") ||
277-
NameEqualToCString(t_token -> value, "true")))))
296+
t_is_bool))
278297
return ParserReport(self, t_token -> start, kParserErrorConstantExpected, nil);
279298

280299
if (!ScannerAdvance(self -> scanner))
281300
return false;
282301

283302
self -> position = t_token -> start;
284303

285-
r_value = t_token -> value;
304+
if (!t_is_bool)
305+
r_value = t_token -> value;
286306

287307
return true;
288308
}

lcidlc/src/Value.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,40 @@ double NumberGetReal(NumberRef self)
328328
}
329329

330330
////////////////////////////////////////////////////////////////////////////////
331+
332+
bool BooleanCreateWithBool(bool value, ValueRef& r_value)
333+
{
334+
bool t_success;
335+
t_success = true;
336+
337+
Value *self;
338+
self = nil;
339+
if (t_success)
340+
t_success = MCMemoryNew(self);
341+
342+
if (t_success)
343+
{
344+
self -> references = 1;
345+
self -> type = kValueTypeBoolean;
346+
self -> boolean = value;
347+
348+
s_names = self;
349+
350+
r_value = self;
351+
}
352+
else
353+
{
354+
MCMemoryDelete(self);
355+
}
356+
357+
return t_success;
358+
359+
}
360+
361+
bool BooleanGetBool(ValueRef self)
362+
{
363+
return self -> boolean;
364+
}
365+
366+
////////////////////////////////////////////////////////////////////////////////
367+

lcidlc/src/Value.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,7 @@ double NumberGetReal(ValueRef value);
7777

7878
////////////////////////////////////////////////////////////////////////////////
7979

80+
bool BooleanCreateWithBool(bool value, ValueRef& r_value);
81+
bool BooleanGetBool(ValueRef value);
82+
8083
#endif

0 commit comments

Comments
 (0)