[Gambas-bugtracker] Bug #2844: Sign Extensions and Negatives Causing Overflows

bugtracker at gambaswiki.org bugtracker at gambaswiki.org
Sun Dec 31 23:29:13 CET 2023


http://gambaswiki.org/bugtracker/edit?object=BUG.2844&from=L21haW4-

REALITYRIPPLE reported a new bug.

Summary
-------

Sign Extensions and Negatives Causing Overflows

Type             : Bug
Priority         : Medium
Gambas version   : 3.18
Product          : Language


Description
-----------

I don't know exactly what version started this problem, but I've started getting overflow errors when working with sign extensions:

  Dim iVal As Long

  iVal = CByte(&H80&)               'No Problem, returns 128 because bytes are unsigned
  iVal = CShort(&H8000)             'No Problem, returns -32768
  iVal = CShort(&H8000&)            'OVERFLOW, should be -32768
  iVal = CInt(&H80000000)           'No Problem, returns -2147483648
  iVal = CInt(&H80000000&)          'OVERFLOW, should be -2147483648
  iVal = CLong(&H8000000000000000)  'No Problem, returns -9223372036854775808
  iVal = CLong(&H8000000000000000&) 'No Problem, returns -9223372036854775808, no idea why

These all worked as expected back in 3.15, which is what my application targets and is compiled on, but users running the 3.18 runtime have started getting crashes.

The same unexpected overflow problem also triggers in conversion contexts:

  Dim iTest As Short
  Dim iTest2 As Integer

  iTest = Lsl(15, 11)          'No Problem, returns 30720
  iTest = Lsl(16, 11)          'OVERFLOW, should be -32768 (short)
  iTest = Lsl(CShort(16), 11)  'No Problem, returns -32768
  iTest2 = Lsl(16, 11)         'No Problem, returns 32768 (expected, but wrong result for the desired math, due to Type being an integer)
  iTest2 = CShort(Lsl(16, 11)) 'OVERFLOW, should be -32768 (stored in an integer)

This is triggering an overflow despite the final values being within 16 bits. My guess is the leading bit is causing the number to be prefixed with null bits, resulting in the passed value being 32-bit, but then failing to be truncated to 16-bit despite the first 16 bits all being 0. This would seem to be backed up by the success of forcing the shifted number to be a Short - if it can't be expanded to 32 bits, it can't fail to truncate.

The documentation for CShort and the Short type both still say that overflows are silently ignored or are truncated, however that definitely no longer seems to be the case.


System information
------------------

[System]
Gambas=3.18.4
OperatingSystem=Linux
Distribution=arch 
Kernel=6.6.7-arch1-1
Architecture=x86_64
Cores=2
Memory=1468M
Language=en_US.UTF-8
Desktop=XFCE
DesktopResolution=96
DesktopScale=7
WidgetTheme=adwaita-dark
Font=Sans,10

[Programs]
gcc=gcc (GCC) 13.2.1 20230801
git=git version 2.43.0

[Libraries]
Cairo=libcairo.so.2.11800.0
Curl=libcurl.so.4.8.0
DBus=libdbus-1.so.3.32.4
GDK2=libgdk-x11-2.0.so.0.2400.33
GDK3=libgdk-3.so.0.2407.32
GStreamer=libgstreamer-1.0.so.0.2208.0
GTK+2=libgtk-x11-2.0.so.0.2400.33
GTK+3=libgtk-3.so.0.2407.32
OpenGL=libGL.so.1.7.0
Poppler=libpoppler.so.133.0.0
QT5=libQt5Core.so.5.15.11
RSvg=librsvg-2.so.2.48.0
SDL=libSDL2-2.0.so.0.2800.5
SQLite 3=libsqlite3.so.0.8.6

[Environment]
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DEBUGINFOD_URLS=https://debuginfod.archlinux.org 
DESKTOP_SESSION=xfce
DISPLAY=:0.0
FREETYPE_PROPERTIES=truetype:interpreter-version=40
GB_GUI=gb.gtk3
GDMSESSION=xfce
GTK_MODULES=canberra-gtk-module:canberra-gtk-module
HOME=<home>
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
LOGNAME=<user>
MAIL=/var/spool/mail/<user>
MOTD_SHOWN=pam
PANEL_GDK_CORE_DEVICE_EVENTS=0
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
PWD=<home>
QT_LOGGING_RULES=*.debug=false
QT_QPA_PLATFORMTHEME=qt5ct
SESSION_MANAGER=local/<hostname>:@/tmp/.ICE-unix/1128,unix/<hostname>:/tmp/.ICE-unix/1128
SHELL=/bin/bash
SHLVL=0
SSH_AGENT_PID=1252
SSH_AUTH_SOCK=/tmp/ssh-XXXXXXcD2bW5/agent.1251
TZ=:/etc/localtime
USER=<user>
XAUTHORITY=<home>/.Xauthority
XDG_CONFIG_DIRS=/etc/xdg
XDG_CURRENT_DESKTOP=XFCE
XDG_DATA_DIRS=/usr/local/share:/usr/share
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/<user>
XDG_MENU_PREFIX=xfce-
XDG_RUNTIME_DIR=/run/user/1000
XDG_SEAT=seat0
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_SESSION_CLASS=user
XDG_SESSION_DESKTOP=xfce
XDG_SESSION_ID=1
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_SESSION_TYPE=x11
XDG_VTNR=7




More information about the Bugtracker mailing list