Jump to content

Error C6385 in C++ (AES)

unsigned char* decryptedmessage = new unsigned char[LenOfPaddedMessage];
        for (int i = 0; i < LenOfPaddedMessage; i++) {
            decryptedmessage[i] = encryptedmessage2[i];
        }

        for (int i = 0; i < LenOfPaddedMessage; i += 16) {
            AES_Decrypt(decryptedmessage + i, inputkey);

        }

        cout << endl << endl;
        cout << "\nDecrypted message:" << endl;
        for (int i = 0; i < LenOfPaddedMessage; i++)
        {
            cout << decryptedmessage[i];
            
        }
        cout << endl << endl;
        
        string AnswerYesNo; // een test/encryptie module
        cout << "\nType 'yes' if you wish to encrypt the message, answer 'no' if not: ";
        getline(cin, AnswerYesNo);
        int AnswerLength;
        AnswerLength = AnswerYesNo.length();

        if (AnswerLength == 3)
        {
            int originalLen = strlen((const char*)decryptedmessage);
            int lenOfPaddedMessage = originalLen;

            if (lenOfPaddedMessage % 16 != 0)
                lenOfPaddedMessage = (lenOfPaddedMessage / 16 + 1) * 16;

            unsigned char* paddedMessage = new unsigned char[lenOfPaddedMessage];
            for (int x = 0; x < lenOfPaddedMessage; x++)
            {
                if (x >= originalLen) paddedMessage[x] = 0;
                else paddedMessage[x] = decryptedmessage[x];
            }

Hi guys this is Austin, I'm having a problem with my C++ project for school (it's making AES, if you're wondering). I'm a massive noob when it comes to coding, let alone C languages, so please have mercy on my soul.

The problem I'm having is in the second to last line: "... else paddedMessage[x] = decryptedmessage[x];" 
The warning Visual gives me is coded C6385. Of course I've looked this up and I understand what the problem might be, but I can't seem to figure out the cause of it. I suspect it's something to do "decryptedmessage", but I'm not sure.The code listed above isn't the entire code (that's something like 600 lines: too much for this post I suppose, but if you need it, just ask), I've chosen to take the code from the 'birth' of decryptedmessage until the error. If you need more, once again, just ask.

I'm sorry for my awful description of the problem and I know I'm a horrible noob. Please help me out, it would mean a lot.
Thanks in advance.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Lennart van de Merwe said:

coded C6385

See here.

 

Basically, it's just warning you there's a chance that 'x' might be a larger value than the declared length of 'decryptedMessage'. Check that x < decryptedMessageLength in your for loop.

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | Mellanox ConnectX-3 10G NIC | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4x 10TB WD Whites / 4x 14TB Seagate Exos / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X540-T2 10G NIC | LSI 9207-8i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / Seagate 1.5TB HDD | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

 

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, AbydosOne said:

See here.

 

Basically, it's just warning you there's a chance that 'x' might be a larger value than the declared length of 'decryptedMessage'. Check that x < decryptedMessageLength in your for loop.

I'm sorry, but what do you mean by that? I'm a noob remember X<

Link to comment
Share on other sites

Link to post
Share on other sites

59 minutes ago, Lennart van de Merwe said:

I'm sorry, but what do you mean by that? I'm a noob remember X<

Basically, if you have something with 10 slots, and you ask it for whatever is in slot 11, the thing isn't going to know how to respond, and so it might throw an error, or crash, or give you what would be in slot 11 if it existed (this is usually the nastiest outcome for various reasons).

Point is, there a chance you might ask the array for something in a position that doesn't exist, and so you need to ensure that your value of 'x' doesn't tell the arrays 'decryptedmessage' or 'paddedMessage' (in that line) to pick from a slot beyond what they have, by making sure that 'x' is always smaller than the length of either array.

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | Mellanox ConnectX-3 10G NIC | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4x 10TB WD Whites / 4x 14TB Seagate Exos / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X540-T2 10G NIC | LSI 9207-8i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / Seagate 1.5TB HDD | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

 

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, AbydosOne said:

Basically, if you have something with 10 slots, and you ask it for whatever is in slot 11, the thing isn't going to know how to respond, and so it might throw an error, or crash, or give you what would be in slot 11 if it existed (this is usually the nastiest outcome for various reasons).

Point is, there a chance you might ask the array for something in a position that doesn't exist, and so you need to ensure that your value of 'x' doesn't tell the arrays 'decryptedmessage' or 'paddedMessage' (in that line) to pick from a slot beyond what they have, by making sure that 'x' is always smaller than the length of either array.

Yes, but that int is determined by taking the length of the decryptedmessage, which then also defines the length of the paddedmessage. Could it be something to do with:
 

int originalLen = strlen((const char*)decryptedmessage);
			int lenOfPaddedMessage = originalLen;
Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Lennart van de Merwe said:

Yes, but that int is determined by taking the length of the decryptedmessage, which then also defines the length of the paddedmessage. Could it be something to do with:
 


int originalLen = strlen((const char*)decryptedmessage);
			int lenOfPaddedMessage = originalLen;

It's a compiler warning, which means that it doesn't always do the most indepth analysis of what the upper bound may be.  It is giving that warning because it just detected that the number might be larger than the original (in this case it is sort of a guideline to look into to make sure it won't).

 

As a note though, strlen is null terminated; so yes originalLen could be greater than the defined length and you could be reading the memory that comes after the decryptedmessage.

int main() {
	char[2] a;
    a[0] = 'a';
    a[1] = 'b';
    int len = strlen(a); //Len is not well defined.  In theory you might think it is 2, but in practice it could be anything
    	//Since it will depend what is stored in the memory location a[2] (sometimes it might be 0, so you might get the right answer)
        //but it isn't guarenteed
	return 0;
}

 

All you could use something like as follows to catch it properly

if(lenOfPaddedMessage > LenOfPaddedMessage)
	lenOfPaddedMessage = LenOfPaddedMessage;

 

*So now this is just my opinion, but it I always try keeping variable names distinct from each other.  Yes, l vs L makes it a different variable but to me it just makes things confusing when they are used together (having to remember which is which as I don't usually process the capitals in my head)

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, wanderingfool2 said:

It's a compiler warning, which means that it doesn't always do the most indepth analysis of what the upper bound may be.  It is giving that warning because it just detected that the number might be larger than the original (in this case it is sort of a guideline to look into to make sure it won't).

 

As a note though, strlen is null terminated; so yes originalLen could be greater than the defined length and you could be reading the memory that comes after the decryptedmessage.


int main() {
	char[2] a;
    a[0] = 'a';
    a[1] = 'b';
    int len = strlen(a); //Len is not well defined.  In theory you might think it is 2, but in practice it could be anything
    	//Since it will depend what is stored in the memory location a[2] (sometimes it might be 0, so you might get the right answer)
        //but it isn't guarenteed
	return 0;
}

 

All you could use something like as follows to catch it properly


if(lenOfPaddedMessage > LenOfPaddedMessage)
	lenOfPaddedMessage = LenOfPaddedMessage;

 

*So now this is just my opinion, but it I always try keeping variable names distinct from each other.  Yes, l vs L makes it a different variable but to me it just makes things confusing when they are used together (having to remember which is which as I don't usually process the capitals in my head)

Thanks a lot for the input and clear explanation. Unfortunately it only makes it worse when it comes to the output of the code, so I'm leaving the bug as it is, especially since I have to start handing in my assignment.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Lennart van de Merwe said:

Thanks a lot for the input and clear explanation. Unfortunately it only makes it worse when it comes to the output of the code, so I'm leaving the bug as it is, especially since I have to start handing in my assignment.

Okay, so I actually read your code...yea, you would actually want to apply the fix to originalLen...or if it allows it strnlen...and cap it there

 

 

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×