Compiler not check wrong *.x passed to Proc(*.y)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Compiler not check wrong *.x passed to Proc(*.y)

Post by skywalk »

This error is hard to find.
Maybe this is a Feature Request, but I hoped the compiler would tell me I passed the wrong datatype to a Procedure before running the code.
ASM produces runtime IMA which can be helpful to find problem.
CBE just quits: "The debugged executable quit unexpectedly." Now you are forced to find the C output error log?

Code: Select all

EnableExplicit
Structure structI
  a.i
  b.i
EndStructure
Structure structStr
  a$
  b$
EndStructure
Procedure.i DoStr(*p.structStr)
  ProcedureReturn Val(*p\b$)  ;<-- IMA
EndProcedure
Procedure.i DoI(*p.structI)
  ProcedureReturn *p\b
EndProcedure
Global.i y
Global xstr.structStr
xStr\b$ = "xStr"
Global xi.structI
xI\b = 2
y = DoI(xStr) ;<-- Compiler accepts wrong datatype for parameter.
Debug y
y = DoStr(xI) ;<-- Compiler accepts wrong datatype for parameter.
              ;<-- And generates a run-time IMA.
Debug y
; [14:17:52] Waiting for executable to start...
; [14:17:52] Executable type: Windows - x64  (64bit, Unicode, Thread, Purifier)
; [14:17:52] Executable started.
; [14:17:52] [ERROR] BUG_v6_compiler-not-catch-bad-proc(ptr.wrongtype).pb (Line: 11)
; [14:17:52] [ERROR] Invalid memory access. (read error at address 2)
; [14:17:57] The Program was killed.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by mk-soft »

PB has no management to check the data types of pointers. It can also be an unknown structure from a third party, or it can also overlay data types, which is very helpful.
Only the C backend should also come back with a better error code.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by skywalk »

Yes, if an overlaid structure or structureunion was passed, that is possible.
Regardless, I want to know with a warning that the structures are not identical.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by STARGÅTE »

What is the bug-report in this topic?
skywalk wrote: Tue Dec 13, 2022 8:30 pm Maybe this is a Feature Request, but I hoped the compiler would tell me I passed the wrong datatype to a Procedure before running the code.
Why you then post in Bugs?
The PureBasic compiler only checks for numeric or string data types, because they are handled differently.
There is no error code given, when you pass a float to a integer argument or a pointer to a float argument.
In your case you pass a pointer (no more no less) and the procedure argument wants a pointer (no more no less).
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
nsstudios
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by nsstudios »

skywalk wrote: ASM produces runtime IMA which can be helpful to find problem. CBE just quits: "The debugged executable quit unexpectedly."
I can't reproduce that, both asm and c backends, 64 and 32 bit report an invalid memory access at line 11 here (Windows 10 22h2).
Perhaps that's something to look into, but according to
Fred (August 5th, 2021, 8:32 am) wrote: Unfortunately the purifier isn't as good with the C backend, as we can't check local variable overflow. The good news is we should now be able to use C specific tools like Valgrind or Purify when compiling in debug mode with symbols.
, the c backend doesn't catch as many errors (maybe I'm just misunderstanding, though).
It would be nice if pb knew about pointer types, but at the very least it should produce ima, because I do agree that it's hard to find a problem if all you get is a crash.
One solution that I thought of would be using SizeOf to verify, but doesn't necessarily help if you have a structure with 8 bytes or another structure with two longs, for example.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by skywalk »

STARGATE - It is a bug in that the CBE compiler gives me nothing but a crash.
All variables including pointers are defined somewhere in the code.
Passing the wrong pointer to a procedure parameter must at least give a warning.
How is that difficult when standard datatypes are not allowed to pass incorrectly?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by STARGÅTE »

skywalk wrote: Wed Dec 14, 2022 6:31 pm Passing the wrong pointer to a procedure parameter must at least give a warning.
No, it doesn't have to! The syntax of the code is valid --> no compiler error. In PureBasic you have a pointer, just a pointer. For PureBasic it is completely irrelevant whats behind the pointer. That's how PureBasic works since version 1.0. The structure definition in the procedure argument is just to have access to specific memory offsets und data formats.
In particular, even passing a structured variable to a procedure is just the short form of: DoI(@xStr) and DoStr(@xI), because you pass not really a structured variable, but just the address.
skywalk wrote: Wed Dec 14, 2022 6:31 pm How is that difficult when standard datatypes are not allowed to pass incorrectly?
It isn't, but it is a request for PureBasic, not a Bug report. Maybe: EnableTypeCheck
But keep in mind, then you have also write something like:
x.f = Float(y.i) instead of x.f = y.i, to avoid a "warning".
Or z.i = Int(Pow(Float(x.i), Float(y.i))) instead of z = Pow(x, y) :? .
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [BUG-v6] Compiler not check wrong *.x passed to Proc(*.y)

Post by skywalk »

Wow, my point here is to help others not have to chase down valid syntax which leads to an IMA.
A warning is not a crazy ask. :?:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply