microsoft / Windows-classic-samples

This repo contains samples that demonstrate the API used in Windows classic desktop applications.
Other
5.1k stars 3.24k forks source link

[Win7Sample.OleDb.RowsetViewer] Writing LONG data in SHORT variable #287

Open dmitry-lipetsk opened 1 year ago

dmitry-lipetsk commented 1 year ago

VS Debugger found a problem with the following code:

https://github.com/microsoft/Windows-classic-samples/blob/7cbd99ac1d2b4a0beffbaba29ea63d024ceff700/Samples/Win7Samples/dataaccess/oledb/rowsetviewer/crowset.cpp#L671-L672

DBTYPE is based on USHORT datatype.

I have not got any problems with this code within 20 years but I think it needs to correct it with:

                LONG lSchemaType; /*DBTYPE == UI2*/
                if(ConvertToLONG(pwszValue, &lSchemaType, 0/*Min*/, USHRT_MAX, 0/*Base*/))
                {
                    //NOTE: on failure we have no choice to leave the column as the original
                    //returned integer.
                    StringCopy(pwszValue, GetDBTypeName((DBTYPE)lSchemaType), ulMaxSize);
                }

I tested this new code under debugger - it works fine.