Skip to content

Commit ce7c7eb

Browse files
committed
Merge pull request #454 from MageKing17/bugfix/lua-wing-indexer-crash
Fix various issues with wings in the Lua interface
2 parents e49159b + 6dd3158 commit ce7c7eb

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

code/parse/lua.cpp

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,7 @@ ADE_VIRTVAR(Name, l_Shiptype, "string", "Ship type name", "string", "Ship type n
32323232
if(!ade_get_args(L, "o|s", l_Shiptype.Get(&idx), &s))
32333233
return ade_set_error(L, "s", "");
32343234

3235-
if(idx < 0 || idx > (int)Ship_types.size())
3235+
if(idx < 0 || idx >= (int)Ship_types.size())
32363236
return ade_set_error(L, "s", "");
32373237

32383238
if(ADE_SETTING_VAR && s != NULL) {
@@ -3913,7 +3913,7 @@ ADE_FUNC(__tostring, l_Weaponclass, NULL, "Weapon class name", "string", "Weapon
39133913
if(!ade_get_args(L, "o|s", l_Weaponclass.Get(&idx), &s))
39143914
return ade_set_error(L, "s", "");
39153915

3916-
if(idx < 0 || idx > Num_weapon_types)
3916+
if(idx < 0 || idx >= Num_weapon_types)
39173917
return ade_set_error(L, "s", "");
39183918

39193919
return ade_set_args(L, "s", Weapon_info[idx].name);
@@ -3925,10 +3925,10 @@ ADE_FUNC(__eq, l_Weaponclass, "weaponclass, weaponclass", "Checks if the two cla
39253925
if(!ade_get_args(L, "oo", l_Weaponclass.Get(&idx1), l_Weaponclass.Get(&idx2)))
39263926
return ade_set_error(L, "b", false);
39273927

3928-
if(idx1 < 0 || idx1 > Num_weapon_types)
3928+
if(idx1 < 0 || idx1 >= Num_weapon_types)
39293929
return ade_set_error(L, "b", false);
39303930

3931-
if(idx2 < 0 || idx2 > Num_weapon_types)
3931+
if(idx2 < 0 || idx2 >= Num_weapon_types)
39323932
return ade_set_error(L, "b", false);
39333933

39343934
return ade_set_args(L, "b", idx1 == idx2);
@@ -3942,7 +3942,7 @@ ADE_VIRTVAR(Name, l_Weaponclass, "string", "Weapon class name", "string", "Weapo
39423942
if(!ade_get_args(L, "o|s", l_Weaponclass.Get(&idx), &s))
39433943
return ade_set_error(L, "s", "");
39443944

3945-
if(idx < 0 || idx > Num_weapon_types)
3945+
if(idx < 0 || idx >= Num_weapon_types)
39463946
return ade_set_error(L, "s", "");
39473947

39483948
if(ADE_SETTING_VAR && s != NULL) {
@@ -3959,7 +3959,7 @@ ADE_VIRTVAR(Title, l_Weaponclass, "string", "Weapon class title", "string", "Wea
39593959
if(!ade_get_args(L, "o|s", l_Weaponclass.Get(&idx), &s))
39603960
return ade_set_error(L, "s", "");
39613961

3962-
if(idx < 0 || idx > Num_weapon_types)
3962+
if(idx < 0 || idx >= Num_weapon_types)
39633963
return ade_set_error(L, "s", "");
39643964

39653965
if(ADE_SETTING_VAR && s != NULL) {
@@ -4004,7 +4004,7 @@ ADE_VIRTVAR(TechTitle, l_Weaponclass, "string", "Weapon class tech title", "stri
40044004
if(!ade_get_args(L, "o|s", l_Weaponclass.Get(&idx), &s))
40054005
return ade_set_error(L, "s", "");
40064006

4007-
if(idx < 0 || idx > Num_weapon_types)
4007+
if(idx < 0 || idx >= Num_weapon_types)
40084008
return ade_set_error(L, "s", "");
40094009

40104010
if(ADE_SETTING_VAR && s != NULL) {
@@ -4021,7 +4021,7 @@ ADE_VIRTVAR(TechAnimationFilename, l_Weaponclass, "string", "Weapon class animat
40214021
if(!ade_get_args(L, "o|s", l_Weaponclass.Get(&idx), &s))
40224022
return ade_set_error(L, "s", "");
40234023

4024-
if(idx < 0 || idx > Num_weapon_types)
4024+
if(idx < 0 || idx >= Num_weapon_types)
40254025
return ade_set_error(L, "s", "");
40264026

40274027
if(ADE_SETTING_VAR && s != NULL) {
@@ -4066,7 +4066,7 @@ ADE_VIRTVAR(Model, l_Weaponclass, "model", "Model", "model", "Weapon class model
40664066
if(!ade_get_args(L, "o|o", l_Weaponclass.Get(&weapon_info_idx), l_Model.GetPtr(&mdl)))
40674067
return ade_set_error(L, "o", l_Model.Set(-1));
40684068

4069-
if(weapon_info_idx < 0 || weapon_info_idx > Num_weapon_types)
4069+
if(weapon_info_idx < 0 || weapon_info_idx >= Num_weapon_types)
40704070
return ade_set_error(L, "o", l_Model.Set(-1));
40714071

40724072
weapon_info *wip = &Weapon_info[weapon_info_idx];
@@ -4087,7 +4087,7 @@ ADE_VIRTVAR(ArmorFactor, l_Weaponclass, "number", "Amount of weapon damage appli
40874087
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
40884088
return ade_set_error(L, "f", 0.0f);
40894089

4090-
if(idx < 0 || idx > Num_weapon_types)
4090+
if(idx < 0 || idx >= Num_weapon_types)
40914091
return ade_set_error(L, "f", 0.0f);
40924092

40934093
if(ADE_SETTING_VAR) {
@@ -4104,7 +4104,7 @@ ADE_VIRTVAR(Damage, l_Weaponclass, "number", "Amount of damage that weapon deals
41044104
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
41054105
return ade_set_error(L, "f", 0.0f);
41064106

4107-
if(idx < 0 || idx > Num_weapon_types)
4107+
if(idx < 0 || idx >= Num_weapon_types)
41084108
return ade_set_error(L, "f", 0.0f);
41094109

41104110
if(ADE_SETTING_VAR) {
@@ -4121,7 +4121,7 @@ ADE_VIRTVAR(FireWait, l_Weaponclass, "number", "Weapon fire wait (cooldown time)
41214121
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
41224122
return ade_set_error(L, "f", 0.0f);
41234123

4124-
if(idx < 0 || idx > Num_weapon_types)
4124+
if(idx < 0 || idx >= Num_weapon_types)
41254125
return ade_set_error(L, "f", 0.0f);
41264126

41274127
if(ADE_SETTING_VAR) {
@@ -4138,7 +4138,7 @@ ADE_VIRTVAR(FreeFlightTime, l_Weaponclass, "number", "The time the weapon will f
41384138
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
41394139
return ade_set_error(L, "f", 0.0f);
41404140

4141-
if(idx < 0 || idx > Num_weapon_types)
4141+
if(idx < 0 || idx >= Num_weapon_types)
41424142
return ade_set_error(L, "f", 0.0f);
41434143

41444144
if(ADE_SETTING_VAR) {
@@ -4155,7 +4155,7 @@ ADE_VIRTVAR(LifeMax, l_Weaponclass, "number", "Life of weapon in seconds", "numb
41554155
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
41564156
return ade_set_error(L, "f", 0.0f);
41574157

4158-
if(idx < 0 || idx > Num_weapon_types)
4158+
if(idx < 0 || idx >= Num_weapon_types)
41594159
return ade_set_error(L, "f", 0.0f);
41604160

41614161
if(ADE_SETTING_VAR) {
@@ -4172,7 +4172,7 @@ ADE_VIRTVAR(Range, l_Weaponclass, "number", "Range of weapon in meters", "number
41724172
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
41734173
return ade_set_error(L, "f", 0.0f);
41744174

4175-
if(idx < 0 || idx > Num_weapon_types)
4175+
if(idx < 0 || idx >= Num_weapon_types)
41764176
return ade_set_error(L, "f", 0.0f);
41774177

41784178
if(ADE_SETTING_VAR) {
@@ -4189,7 +4189,7 @@ ADE_VIRTVAR(Mass, l_Weaponclass, "number", "Weapon mass", "number", "Weapon mass
41894189
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
41904190
return ade_set_error(L, "f", 0.0f);
41914191

4192-
if(idx < 0 || idx > Num_weapon_types)
4192+
if(idx < 0 || idx >= Num_weapon_types)
41934193
return ade_set_error(L, "f", 0.0f);
41944194

41954195
if(ADE_SETTING_VAR) {
@@ -4206,7 +4206,7 @@ ADE_VIRTVAR(ShieldFactor, l_Weaponclass, "number", "Amount of weapon damage appl
42064206
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
42074207
return ade_set_error(L, "f", 0.0f);
42084208

4209-
if(idx < 0 || idx > Num_weapon_types)
4209+
if(idx < 0 || idx >= Num_weapon_types)
42104210
return ade_set_error(L, "f", 0.0f);
42114211

42124212
if(ADE_SETTING_VAR) {
@@ -4223,7 +4223,7 @@ ADE_VIRTVAR(SubsystemFactor, l_Weaponclass, "number", "Amount of weapon damage a
42234223
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &f))
42244224
return ade_set_error(L, "f", 0.0f);
42254225

4226-
if(idx < 0 || idx > Num_weapon_types)
4226+
if(idx < 0 || idx >= Num_weapon_types)
42274227
return ade_set_error(L, "f", 0.0f);
42284228

42294229
if(ADE_SETTING_VAR) {
@@ -4240,7 +4240,7 @@ ADE_VIRTVAR(TargetLOD, l_Weaponclass, "number", "LOD used for weapon model in th
42404240
if(!ade_get_args(L, "o|i", l_Weaponclass.Get(&idx), &lod))
42414241
return ade_set_error(L, "i", 0);
42424242

4243-
if(idx < 0 || idx > Num_weapon_types)
4243+
if(idx < 0 || idx >= Num_weapon_types)
42444244
return ade_set_error(L, "i", 0);
42454245

42464246
if(ADE_SETTING_VAR) {
@@ -4257,7 +4257,7 @@ ADE_VIRTVAR(Speed, l_Weaponclass, "number", "Weapon max speed, aka $Velocity in
42574257
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &spd))
42584258
return ade_set_error(L, "f", 0.0f);
42594259

4260-
if(idx < 0 || idx > Num_weapon_types)
4260+
if(idx < 0 || idx >= Num_weapon_types)
42614261
return ade_set_error(L, "f", 0.0f);
42624262

42634263
if(ADE_SETTING_VAR) {
@@ -4274,7 +4274,7 @@ ADE_VIRTVAR(Bomb, l_Weaponclass, "boolean", "Is weapon class flagged as bomb", "
42744274
if(!ade_get_args(L, "o|b", l_Weaponclass.Get(&idx), &newVal))
42754275
return ADE_RETURN_FALSE;
42764276

4277-
if(idx < 0 || idx > Num_weapon_types)
4277+
if(idx < 0 || idx >= Num_weapon_types)
42784278
return ADE_RETURN_FALSE;
42794279

42804280
weapon_info *info = &Weapon_info[idx];
@@ -4305,7 +4305,7 @@ ADE_VIRTVAR(CargoSize, l_Weaponclass, "number", "The cargo size of this weapon c
43054305
if(!ade_get_args(L, "o|f", l_Weaponclass.Get(&idx), &newVal))
43064306
return ade_set_args(L, "f", -1.0f);
43074307

4308-
if(idx < 0 || idx > Num_weapon_types)
4308+
if(idx < 0 || idx >= Num_weapon_types)
43094309
return ade_set_args(L, "f", -1.0f);
43104310

43114311
weapon_info *info = &Weapon_info[idx];
@@ -8214,7 +8214,7 @@ struct sound_entry_h
82148214

82158215
bool IsValid()
82168216
{
8217-
if (idx < 0 || idx > (int) Snds.size())
8217+
if (idx < 0 || idx >= (int) Snds.size())
82188218
return false;
82198219

82208220
return true;
@@ -8797,15 +8797,15 @@ ADE_FUNC(isValid, l_Message, NULL, "Checks if the message handle is valid", "boo
87978797
//**********HANDLE: Wing
87988798
ade_obj<int> l_Wing("wing", "Wing handle");
87998799

8800-
ADE_INDEXER(l_Wing, "number Index", "Array of ships in the wing", "ship", "Ship handle, or invalid ship handle if index is invawing handle is invalid")
8800+
ADE_INDEXER(l_Wing, "number Index", "Array of ships in the wing", "ship", "Ship handle, or invalid ship handle if index is invalid or wing handle is invalid")
88018801
{
88028802
int wdx;
88038803
int sdx;
88048804
object_h *ndx=NULL;
88058805
if(!ade_get_args(L, "oi|o", l_Wing.Get(&wdx), &sdx, l_Ship.GetPtr(&ndx)))
88068806
return ade_set_error(L, "o", l_Ship.Set(object_h()));
88078807

8808-
if(sdx < 1 || sdx > Wings[wdx].current_count) {
8808+
if(wdx < 0 || wdx >= Num_wings || sdx < 1 || sdx > Wings[wdx].current_count) {
88098809
return ade_set_error(L, "o", l_Ship.Set(object_h()));
88108810
}
88118811

@@ -8819,11 +8819,11 @@ ADE_INDEXER(l_Wing, "number Index", "Array of ships in the wing", "ship", "Ship
88198819
return ade_set_args(L, "o", l_Ship.Set(object_h(&Objects[Ships[Wings[wdx].ship_index[sdx]].objnum])));
88208820
}
88218821

8822-
ADE_FUNC(__len, l_Wing, NULL, "Number of wings in mission", "number", "Number of wings in mission")
8822+
ADE_FUNC(__len, l_Wing, NULL, "Gets the number of ships in the wing", "number", "Number of ships in wing, or 0 if invalid handle")
88238823
{
88248824
int wdx;
8825-
if(!ade_get_args(L, "o", l_Wing.Get(&wdx)))
8826-
return ade_set_error(L, "i", NULL);
8825+
if(!ade_get_args(L, "o", l_Wing.Get(&wdx)) || wdx < 0 || wdx >= Num_wings)
8826+
return ade_set_error(L, "i", 0);
88278827

88288828
return ade_set_args(L, "i", Wings[wdx].current_count);
88298829
}
@@ -8832,7 +8832,7 @@ ADE_VIRTVAR(Name, l_Wing, "string", "Name of Wing", "string", "Wing name, or emp
88328832
{
88338833
int wdx;
88348834
char *s = NULL;
8835-
if ( !ade_get_args(L, "o|s", l_Wing.Get(&wdx), &s) )
8835+
if ( !ade_get_args(L, "o|s", l_Wing.Get(&wdx), &s) || wdx < 0 || wdx >= Num_wings )
88368836
return ade_set_error(L, "s", "");
88378837

88388838
if(ADE_SETTING_VAR && s != NULL) {
@@ -8842,6 +8842,18 @@ ADE_VIRTVAR(Name, l_Wing, "string", "Name of Wing", "string", "Wing name, or emp
88428842
return ade_set_args(L, "s", Wings[wdx].name);
88438843
}
88448844

8845+
ADE_FUNC(isValid, l_Wing, NULL, "Detects whether handle is valid", "boolean", "true if valid, false if handle is invalid, nil if a syntax/type error occurs")
8846+
{
8847+
int idx;
8848+
if(!ade_get_args(L, "o", l_Wing.Get(&idx)))
8849+
return ADE_RETURN_NIL;
8850+
8851+
if (idx < 0 || idx >= Num_wings)
8852+
return ADE_RETURN_FALSE;
8853+
8854+
return ADE_RETURN_TRUE;
8855+
}
8856+
88458857
//**********HANDLE: Ship
88468858
ade_obj<object_h> l_Ship("ship", "Ship handle", &l_Object);
88478859

@@ -14895,7 +14907,8 @@ ADE_INDEXER(l_Mission_Wings, "number Index/string WingName", "Wings in the missi
1489514907
if(!ade_get_args(L, "*s", &name))
1489614908
return ade_set_error(L, "o", l_Wing.Set(-1));
1489714909

14898-
int idx = wing_name_lookup(name);
14910+
//MageKing17 - Make the count-ignoring version of the lookup and leave checking if the wing has any ships to the scripter
14911+
int idx = wing_lookup(name);
1489914912

1490014913
if(idx < 0)
1490114914
{

0 commit comments

Comments
 (0)