.net C code ported to the AVR for SAS client token

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All

 

I am working on a MQTT  project , where a where my device the client  will need to generate a SAS token. The original code was written in C on the .net compiler. I have manged to get it working on the AVR but at times my AVR just hangs in some code. I am suspecting this is due to how the code handles the allocated memory.

 

Can you some one advice me if the following functions are handle appropriately by the AVR compiler and if not what must i change?

 

These are samples of them. Please note this is the not the full code, but i would like to know that once this code in a AVR how does the device handle them.

 

h = malloc(sizeof(CONECTIONSTRINGSTRUCT));

memset(h->keywords, 0, sizeof(char*) * tokenCount);


tolower(keywordStart[i]);

free(h->keywords[i]);

	if ((snprintf(tokenExpiryStr, sizeof(tokenExpiryStr), "%lu", tokenExpiry)) > sizeof(tokenExpiryStr))
	return -1;
	
	int uriLen = 1 + strlen(GetKeywordValue(h, ("Hostname"))) + strlen(("/devices/")) + strlen(devicedata.iot_imei);
	
		snprintf(toSign, toSignLen, "%s\n%s", encodedUri, tokenExpiryStr);

 

Thanks

Regards

DJ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

djoshi wrote:
I am suspecting this is due to how the code handles the allocated memory.

 

malloc() is generally best avoided on the likes of AVRs.

 

Especially a lot of malloc() and free() - which gives you a high risk of heap fragmentation...

 

Why not just make it a "normal" variable?

 

EDIT

 

if you are going to use malloc(), then you must always check the return value before you use the pointer!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Mar 13, 2020 - 04:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

if you are going to use malloc(), then you must always check the return value before you use the pointer!

 

This!

 

On PCs there's a general assumption that malloc will NEVER run out (because of virtual memory even if you allocate gigabytes and then some more it will still work as the PC will start to page out to disk (very slowly and with a lot of thrashing!). PC programmers therefore get lazy and assume that malloc() simply cannot return 0 so they don't even program for that eventuality. But in an AVR with 1..2K of SRAM or whatever it is very very likely malloc() will run out and when it does it will start returning 0's. So you can't do:

h = malloc(sizeof(CONECTIONSTRINGSTRUCT));

memset(h->keywords

because at some stage that will be:

h = malloc(sizeof(CONECTIONSTRINGSTRUCT));

memset(0->keywords

which is not a good place to be. So after the malloc test h==0 or (more technically correct) h=NULL(/nullptr) and if it is abort and take remedial action.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It runs for few cycles of SAS tokens, but the hangs after some time. I am going to go through it a change it and change it to variables . 

 

So a code such as 

CONNECTIONSTRINGHANDLE csh = CreateConnectionStringHandle(("545435435435643534534"));
		char* password = NULL;
		int passwordLen;
		int saveLen = 0;

		// SAS tokens are not a fixed size. It is possible that the new SAS token
		// will be longer than the previous estimate. Keep increasing the buffer
		// until it fits.
		while (saveLen < (passwordLen = generatePassword(csh, 300, password, saveLen)))
		{
			free(password);
			password = (char*)malloc(passwordLen);
			saveLen = passwordLen;
		}	

How would malloc part be re- written?

Thanks

Regards

DJ

Last Edited: Fri. Mar 13, 2020 - 06:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

djoshi wrote:
How would malloc part be re- written?

It wouldn't - you just get rid of it!

 

So, instead of 

h = malloc(sizeof(CONECTIONSTRINGSTRUCT));

 

You have a normal variable:

CONECTIONSTRINGSTRUCT my_connection;  // Define a variable in the normal way

 

and use it in the normal way:

memset( &my_connection.keywords, 0, sizeof(char*) * tokenCount);

 

and

int uriLen = 1 + strlen(GetKeywordValue( &my_connection, ("Hostname"))) + strlen(("/devices/")) + strlen(devicedata.iot_imei);

etc, ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Mar 13, 2020 - 05:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

By the way, which AVR are we talking about here?

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about

	password = (char*)malloc(passwordLen);		

Would this be rewritten as:

			password =(char*) &passwordLen;				

 

Thanks

Regards

DJ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't seem to understand what malloc() is doing here.

 

djoshi wrote:

	password = (char*)malloc(passwordLen);		

That dynamically allocates a blob of memory of size 'passwordLen', and sets 'password' to point to it.

 

Would this be rewritten as:

			password =(char*) &passwordLen;

No!

 

That is just casting the 'passwordLen' value to be a pointer!

 

Presumably, you want something like this

char password[ passwordLen ];		// a char array of length 'passwordLen'

assuming that 'passwordLen' is a compile-time constant.

 

EDIT

 

typo

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Mar 13, 2020 - 06:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Recent thread on the basics of Dynamic memory allocation:

 

https://www.avrfreaks.net/forum/dynanamic-memory-allocation-what-malloc-does

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

By the way, which AVR are we talking about here?

 

 

atxmega256a3bu

Thanks

Regards

DJ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The originally part of the code is

 

		while (saveLen < (passwordLen = generatePassword(csh, 300, password, saveLen)))
		{
			free(password);
			password = (char*)malloc(passwordLen);							
		}	

 

Thanks

Regards

DJ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so that's trying to free an old password, and create a new one?

 

Without dynamic allocation, you would just have the password as a normal variable - no need to keep re-creating it.

 

If you want to clear-out previous data, use memset() or similar.

 

Note that free() does not clear the memory - so the previous password is still present!

 

To be more secure, you should clear it before freeing it ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...