Want error instead of warning "assignment makes pointer from integer without a cast"

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

I am using Atmel Studio 6.0 (with AVR-GCC) and discovered that this code will give a warning:

uint8_t *tx_len;
tx_len = 20;

 

The warning is "assignment makes pointer from integer without a cast [enabled by default]". Is there any way I can turn this into an error instead? I Googled around a little but failed to find anything that worked.

/Jakob Selbing

Last Edited: Thu. Aug 18, 2016 - 10:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jaksel wrote:
 I Googled around a little but failed to find anything that worked.

It would be helpful if you said what you'd tried - so that people don't waste their time & yours suggesting things that you've already tried ...

 

Did you see this:

 

-Werror

-Werror

Make all warnings into errors. 

 

-Werror=

-Werror=


-Werror=

Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)

Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything. 

-

https://gcc.gnu.org/onlinedocs/g...

 

 

Doesn't AS studio have an option to stop the build on warnings?

 

Edit:

Sorry for the formatting in the quote - another case where the so-called "WYSIWYG editor" is not.

 

angry

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: Wed. Aug 17, 2016 - 11:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I saw that. I'd rather not turn all warnings into errors, but then I need to know the specifier for this warning. The built output does not show the specifier.

 

This is the build output:

 

Building file: .././msg_com.c
        Invoking: AVR/GNU C Compiler : (AVR_8_bit_GNU_Toolchain_3.4.0_663) 4.6.2
        "C:\Program Files (x86)\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.0.65\AVRToolchain\bin\avr-gcc.exe" -funsigned-char -funsigned-bitfields -DPART_ATMEGA -DSPI_TX_ONLY -DAS1108_DIGITS=3 -DSPI_BUFSIZE=2 -DSPI_MSBIT_FIRST -DSPI_SS_CTL -DSPI_CLK_RATE=2 -DMC2_REV0_DISPLAY_SEGMENT_MAPPING -DF_CPU=10000000L -DUART_TEMP_REPORT -DUART_DEBUG_I2C_FAILURE -DUART_DEBUG_I2C_CRC_ERROR -DUART_DEBUG_I2C_RESTART_COUNT -DUART_DEBUG_UVLED_INT_SIGNAL -DUART_DEBUG_UVLED_INFO_INIT  -I"..\." -I"../mc2lib" -I"../rc2lib"  -Os -fpack-struct -fshort-enums -g2 -Wall -c -gdwarf-2 -std=gnu99 -fdiagnostics-show-option -MD -MP -MF "msg_com.d" -MT"msg_com.d" -MT"msg_com.o"  -mmcu=atmega328   -o"msg_com.o" ".././msg_com.c" 
        .././msg_com.c: In function 'msg_com_encode_tx':
[long file path here...]\msg_com.c(157,9): assignment makes pointer from integer without a cast [enabled by default]
        Finished building: .././msg_com.c

/Jakob Selbing

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

Go on.   Anyone would panic if they saw you do that sort of operation.

 

I would prefer it to be an Error rather than a Warning.

 

OTOH,   there are occasions when you force a scalar value into a pointer.    In those situations, you use a cast AND add a comment.

But it is pretty unlikely that you would choose a name like "tx_len" for a pointer in the first place.

 

Hey-ho,  no Lint or Compiler can control the intuitiveness of identifier names.

 

David.

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

david.prentice wrote:
Anyone would panic if they saw you do that sort of operation.

I did!

 

How can you possibly know what is at address 20 anyway? If you really want to assign 20 to the pointer make the 20 into a pointer value:

uint8_t *tx_len;
tx_len = (uint8_t *)20;

but this is HUGELY dangerous code. So what do you actually intend to do with this pointer (set to 20) now that you have it? If you do something like:

*tx_len = 123;

you are now going to write 123 into memory location 20. I happen to know that address 20 in most AVRs actually exposes the content of AVR register R20. So if you did this you would now have written 20 into register 20. Do you even know what the compiler might be using R20 for at this time? You probably just corrupt something really important.

 

Or was this some clever way for you to be able to read R20 from C code:

myCopyR20 = *tx_len;

I cannot help wondering if what you are trying to do here is create 20 bytes of storage? If that's what you want aren't the obvious solutions:

const uint8_t tx_len = 20;
uint8_t tx_buffer[tx_len];

or if you really want to mess with pointers:

const uint8_t tx_len = 20;
uint8_t * tx_buffer = (uint8_t *) malloc(tx_len);

Anyway - you CLEARLY don't mean what it appears you are attempting in #1 . It is dangerous code.

Last Edited: Wed. Aug 17, 2016 - 01:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yea. Warnings are errors. All of them.

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

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

Something you can achieve with -Werror. However the assignment of an int as a pointer value is not necessarily always "fatal". It's just OP here does seem to understand AVR addressing - location 0..13 is a very dangerous place in most AVRs. Actually you could probably say the same about 0x20..0x5F or 0x20.0xFF in a lot of them too!

 

An example where the assignment of 20 to a pointer that isn't so dangerous might be something like:

uint8_t * myEElocation = 20;
eeprom_update_byte(myEELocation, 123);

This too would prompt "assignment makes pointer from integer without cast" yet, as long as one was careful, the 20 in this case would be fairly benign. However I guess you could still use -Werror and modify the code to be:

uint8_t * myEElocation = (uint8_t *)20;
eeprom_update_byte(myEELocation, 123);

so the warning/(error) does not occur.

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

clawson wrote:
How can you possibly know what is at address 20 anyway?

Easy now, troops.

 

OP  might just have been posting an arbitrary fragment demonstrating the situation.  Just because "foo" and "bar" aren't used...

 

And I suspect that OP's slip-up was adding the * to tx_len definition.  So [my guess]  there was no intent to have tx_len be a pointer or to assign an arbitrary number as an address.

 

But you gurus can still help, answering for -Werror=

 

jaksel wrote:
I need to know the specifier for this warning. The built output does not show the specifier.

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Now y'all are going to make me try to figger out how the infinite-value toolchain works.  So far I've found that you use the warning name:

https://gcc.gnu.org/onlinedocs/g...

-Werror=

Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above.

 Next step is to try to find the list of warning names...

 

Scrolling down a bit from the above quote, -Wall has a list which includes...

 

-Wall turns on the following warning flags:

          -Waddress   
          -Warray-bounds=1 (only with -O2)  
          -Wbool-compare  
          -Wc++11-compat  -Wc++14-compat
          -Wchar-subscripts  
          -Wcomment  
          -Wduplicate-decl-specifier (C and Objective-C only) 
          -Wenum-compare (in C/ObjC; this is on by default in C++) 
          -Wformat   
          -Wimplicit (C and Objective-C only) 
          -Wimplicit-int (C and Objective-C only) 
          -Wimplicit-function-declaration (C and Objective-C only) 
          -Winit-self (only for C++) 
          -Wlogical-not-parentheses
          -Wmain (only for C/ObjC and unless -ffreestanding)  
          -Wmaybe-uninitialized 
          -Wmemset-elt-size 
          -Wmemset-transposed-args 
          -Wmisleading-indentation (only for C/C++) 
          -Wmissing-braces (only for C/ObjC) 
          -Wnarrowing (only for C++)  
          -Wnonnull  
          -Wnonnull-compare  
          -Wopenmp-simd 
          -Wparentheses  
          -Wpointer-sign  
          -Wreorder   
          -Wreturn-type  
          -Wsequence-point  
          -Wsign-compare (only in C++)  
          -Wsizeof-pointer-memaccess 
          -Wstrict-aliasing  
          -Wstrict-overflow=1  
          -Wswitch  
          -Wtautological-compare  
          -Wtrigraphs  
          -Wuninitialized  
          -Wunknown-pragmas  
          -Wunused-function  
          -Wunused-label     
          -Wunused-value     
          -Wunused-variable  
          -Wvolatile-register-var 
          

...but I don't see a match to OP's warning.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:

clawson wrote:
How can you possibly know what is at address 20 anyway?

Easy now, troops.

@clawson: R20 for a lot of AVRs.
Quote:

 

OP  might just have been posting an arbitrary fragment demonstrating the situation.  Just because "foo" and "bar" aren't used...

Then he should have used fred, greg or hank.

 

 

As for not telling us everything he tried, so what?

I got the impression that OP did not find anything to try.

What if he did?

Shirley, this is a fairly standard thing to do and any old hands could answer without doing any research.

It seems to me that it belongs in a sticky.

"SCSI is NOT magic. There are *fundamental technical
reasons* why it is necessary to sacrifice a young
goat to your SCSI chain now and then." -- John Woods

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

I think I need to clarify this: the code example in my first post is logically incorrect because I made a typo. But my issue is that it only gives a warning, and I would rather see an error, since the code is - as some of you pointed out - extremely dangerus.

 

FYI, what I actually wanted was this:

uint8_t *tx_len;
*tx_len = 20;

/Jakob Selbing

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

Lee,

 

Just -Werror is quite enough - it will turn all warnings into errors:

$ cat avr.c
#include <avr/io.h>

uint8_t * p = 20;

int main(void) {
}
$ avr-gcc -mmcu=atmega16 -Os avr.c -o avr.elf
avr.c:3:15: warning: initialization makes pointer from integer without a cast
$ avr-gcc -Werror -mmcu=atmega16 -Os avr.c -o avr.elf
cc1: warnings being treated as errors
avr.c:3:15: error: initialization makes pointer from integer without a cast

There is some info about turning this specific warning into an error here:

 

http://stackoverflow.com/questio...

 

See the first answer - so apparently you cannot change this single warning to an error.

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

OK thanks, I think I will use -Werror then. It's a bit annoying though, since it makes little sense to make that kind of assignment without an explicit cast.

/Jakob Selbing

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

Not sure I understand your point. As you say,  if you ever assign a non-zero to a pointer you will always cast so would you not want the warning/(error) to say "hey buddy, you forgot the cast"? 

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

Of couse! My point is that this warning should be an error (or at least that's how I would want it).

 

Why have a warning only for something that is semanitcally incorrect (wrong type) and extremely dangerous, while at the same time very easy to fix (using cast) if the code is as intended? I did run the code a couple of times and had strange run-time errors, because I did not spot the warning in the output window.

 

Sure, I can use -Wall, but only as long as all warnings I get are resolvable.

/Jakob Selbing

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

Did you try

 

-Werror=int-conversion

 

?

Bob.

Last Edited: Thu. Aug 18, 2016 - 08:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yup, that is the one.

C:\SysGCC\avr\bin>avr-gcc -Werror=int-conversion -mmcu=atmega16 test.c -o test.e
lf
test.c:3:15: error: initialization makes pointer from integer without a cast [-W
error=int-conversion]
 uint8_t * p = 20;
               ^
cc1.exe: some warnings being treated as errors

How do you find out which one is relevant?

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

jaksel wrote:

Of couse! My point is that this warning should be an error (or at least that's how I would want it).

 

But surely, whilst dangerous, it's perfectly valid C and is therefore not an error in the sense that the compiler understands errors. Isn't this the sort of thing that Lint is meant for?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

clawson wrote:
How do you find out which one is relevant?

lol, indeed -- the snake gave us a fish.

 

Now, with that information will Google help us learn how to fish?

 

-- I then found "pointer-to-int-castpointer-to-int-castpointer-to-int-cast"

-- LOL again:  it is on the same page I linked to above, further down.

-- (so why isn't it part of Wall?  What part of "all" don't I understand?)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

I think there's a level of warning above "all" which is "pedantic"

 

(who thinks this stuff up!! ;-)

 

When you then read the description of "all" it says:

This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid

To be honest that wouldn't be my own interpretation of "all". I can't help thinking this is one of these things that has grown organically rather than by strict design.

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

To be clear, warnings are about things the compiler suspects of being unwise,

but is willing to accept.

Usually, but not always, they are perfectly valid.

"SCSI is NOT magic. There are *fundamental technical
reasons* why it is necessary to sacrifice a young
goat to your SCSI chain now and then." -- John Woods