Thread: Quick check for potential problems

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048

    Quick check for potential problems

    While I'm waiting on a response in another thread I'll quickly ask if anyone sees any potential problems with this freshly made code:
    Code:
    int alu_mov( alu_t *alu, uintptr_t num, uintptr_t val, size_t size )
    {
    	int ret = 0;
    	void *dst, *src;
    	
    	if ( num >= ALU_REG_ID_LIMIT )
    	{		
    		if ( val >= ALU_REG_ID_LIMIT )
    		{
    			errno = 0;
    			(void)memmove( (void*)num, (void*)val, size );
    			ret = errno;
    			return ret;
    		}
    		
    		ret = _alu_check2( alu, ALU_REG_ID_NEED, val );
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			return ret;
    		}
    		
    		dst = (void*);
    		src = (alu->regv + val)->part;
    		
    		goto copy;
    	}
    	
    	ret = _alu_check1( alu, num );
    	
    	if ( size > alu->buff.perN )
    		size = alu->buff.perN;
    		
    	dst = (alu->regv + num)->part;
    	src = (void*)val;
    	
    	copy:
    	errno = 0;
    	(void)memset( dst, 0, size );
    	ret = errno;
    	
    	if ( ret != 0 )
    	{
    		alu_error( ret );
    		return ret;
    	}
    	
    	if ( size > alu->buff.perN )
    		size = alu->buff.perN;
    		
    	errno = 0;
    	(void)memmove( dst, src, size );
    	ret = errno;
    	
    	if ( ret != 0 )
    		alu_error( ret );
    	
    	return ret;
    }
    Personally I don't but never hurts to ask for 2nd opinion

    Edit: Just spotted the
    Code:
    		dst = (void*);
    line, rectified in file, left here for future readers
    Last edited by awsdert; 08-09-2020 at 01:42 PM.

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Doesn't look like anyone was gonna respond (no biggie in this case) however I did experience some problems with it, eventually went with this:
    Code:
    int alu_mov( alu_t *alu, uintptr_t num, uintptr_t val, size_t size )
    {
    	int ret = 0, tmp = -1;
    	void *ptr;
    	alu_reg_t *REG;
    	
    	if ( num >= ALU_REG_ID_LIMIT )
    	{
    		if ( val >= ALU_REG_ID_LIMIT )
    		{
    			errno = 0;
    			(void)memmove( (void*)num, (void*)val, size );
    			ret = errno;
    			return ret;
    		}
    		
    		ret = alu_check2( alu, ALU_REG_ID_NEED, val );
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			return ret;
    		}
    		
    		ret = alu_get_reg( alu, &tmp );
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			return ret;
    		}
    		
    		ptr = (void*)num;
    		REG = (alu->regv + tmp);
    		
    		alu__or( alu, tmp, val );
    		
    		(void)memset( ptr, 0, size );
    		
    		if ( size > alu->buff.perN )
    			size = alu->buff.perN;
    		
    		(void)memmove( ptr, REG->part, size );
    		
    		(void)alu_rem_reg( alu, tmp );
    		return 0;
    	}
    	
    	if ( val >= ALU_REG_ID_LIMIT )
    	{	
    		ret = alu_check1( alu, num );
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			return ret;
    		}
    		
    		ret = alu_get_reg( alu, &tmp );
    		
    		if ( ret != 0 )
    		{
    			alu_error( ret );
    			return ret;
    		}
    		
    		ptr = (void*)val;
    		REG = (alu->regv + tmp);
    		
    		if ( size > alu->buff.perN )
    			size = alu->buff.perN;
    		
    		(void)memmove( REG->part, ptr, size );
    		
    		(void)alu_xor( alu, num, num );
    		(void)alu__or( alu, num, tmp );
    		
    		(void)alu_rem_reg( alu, tmp );
    		return 0;
    	}
    	
    	alu_xor( alu, num, num );
    	alu__or( alu, num, val );
    	
    	return ret;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by awsdert
    Just spotted the
    Code:
    dst = (void*);
    Isn't that the kind of typo a compiler or even an IDE's code analysis will tell you about? You shouldn't need to ask for help in finding that kind of mistakes in a forum.

    Quote Originally Posted by awsdert
    Doesn't look like anyone was gonna respond (no biggie in this case)
    We're part of the same online forum community, but that doesn't mean we're team mates who already have some idea of what your code does. So, by not providing context, e.g., properly documenting your code, you're not making it any easier for potential helpers to want to bother with your code.
    Last edited by laserlight; 08-09-2020 at 03:58 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by laserlight View Post
    Isn't that the kind of typo a compiler or even an IDE's code analysis will tell you about? You shouldn't need to ask for help in finding that kind of mistakes in a forum.
    It wasn't the kind of bug I was asking for a check for, I just figured someone would eventually bring it up so headed it off.

    Quote Originally Posted by laserlight View Post
    We're part of the same online forum community, but that doesn't mean we're team mates who already have some idea of what your code does. So, by not providing context, e.g., properly documenting your code, you're not making it any easier for potential helpers to want to bother with your code.
    Wasn't the function name enough context? ALU standing for Arithmetic Logic Unit and then mov was clearly a reference to the mov cpu instruction

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by awsdert
    Wasn't the function name enough context? ALU standing for Arithmetic Logic Unit and then mov was clearly a reference to the mov cpu instruction
    Nope. People don't necessarily immediately know what "ALU" stands for without context (I only happened to know from reading your previous posts), and without that it is easy to guess that "mov" means "move" but not so easy to guess what does a "move" actually mean to you here. Then even knowing what alu_mov means, do you really expect the reader to know what's the background behind what looks like an attempt to simulate opcodes and to guess what exactly what the parameters mean?

    Sure, go ahead and continue posting these if they help you: sometimes just asking for help can be enough to help you get an "aha!" moment with your own code. But if you really want random community members to help you, you need to look at things from the fresh perspective of someone who might have no clue what you've been doing, having seen you and your post for the very first time. If they have an introduction into what you're trying to do, can read documentation at least for the functions that are part of your library's interface, can compile and run test code... you'll be putting yourself at an advantage to receive help. It's no guarantee you'll get help, of course, but even if you don't you'll be making your library more usable and it'll help you go through your code from a fresh perspective, which can help uncover unwarranted assumptions etc.

    If you really don't want to put in the effort to do this, that's fine too, but chances are higher people are going to gloss over your code, go "what is this", and move on.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by laserlight View Post
    Nope. People don't necessarily immediately know what "ALU" stands for without context (I only happened to know from reading your previous posts), and without that it is easy to guess that "mov" means "move" but not so easy to guess what does a "move" actually mean to you here. Then even knowing what alu_mov means, do you really expect the reader to know what's the background behind what looks like an attempt to simulate opcodes and to guess what exactly what the parameters mean?

    Sure, go ahead and continue posting these if they help you: sometimes just asking for help can be enough to help you get an "aha!" moment with your own code. But if you really want random community members to help you, you need to look at things from the fresh perspective of someone who might have no clue what you've been doing, having seen you and your post for the very first time. If they have an introduction into what you're trying to do, can read documentation at least for the functions that are part of your library's interface, can compile and run test code... you'll be putting yourself at an advantage to receive help. It's no guarantee you'll get help, of course, but even if you don't you'll be making your library more usable and it'll help you go through your code from a fresh perspective, which can help uncover unwarranted assumptions etc.

    If you really don't want to put in the effort to do this, that's fine too, but chances are higher people are going to gloss over your code, go "what is this", and move on.
    I figured anyone with an interest in programming would also have an interest in how the cpu understands that code and come across the ALU naturally, am I mistaken there?

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    425
    Quote Originally Posted by awsdert View Post
    I figured anyone with an interest in programming would also have an interest in how the cpu understands that code and come across the ALU naturally, am I mistaken there?
    Not everyone with an interest in programming is interested in the details of how the CPU works. Whilst I was of the generation that needed to use machine code to get acceptable performance out of the 8 bit microcomputers we played with, I haven't touched assembly code for years now.

    If you are interested in mathematics, or in business, or in web programming, you're unlikely to have much contact with the CPU. Graphics, yes, there's still a call for assembly programming there, but only if you do the low level stuff.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    80
    I have seen many of those posts,is there a final goal like some cpu emulator,a final big switch(opcode)/case with opcodes list, calling your library?
    you tell me you can C,why dont you C your own bugs?

  9. #9
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by I C everything View Post
    I have seen many of those posts,is there a final goal like some cpu emulator,a final big switch(opcode)/case with opcodes list, calling your library?
    Well I plan to use it both in the hobby c compiler I'm trying to make and in gasp, a separate project I started because I got tired of how slow and unresponsive GameConqueror is. For the compiler the project is needed just for reading in floats/double/long doubles and basically any sized FPN (cause I already made a version that works on slower code with no fixed size). For gasp it's just a case of supporting extra because I can.

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Switching topic, I made this for the char section in my fallback limits header and figured I might as well ask if anyone see's any problems
    Code:
    #ifndef CHAR_SIGNED
    # if defined( __CHAR_UNSIGNED__ )
    # elif defined( __CHAR_SIGNED__ )
    #  define CHAR_SIGNED
    # elif ('\0'|(1 << (CHAR_BIT-1))) < 0
    #  define CHAR_SIGNED
    # endif
    #endif
    
    #ifndef CHAR_UNSIGNED
    # ifndef CHAR_SIGNED
    #  define CHAR_UNSIGNED
    # endif
    #endif
    
    #ifndef CHAR_MAX
    # ifdef CHAR_SIGNED
    #  define CHAR_MAX SCHAR_MAX
    # else
    #  define CHAR_MAX UCHAR_MAX
    # endif
    #endif
    
    #ifndef CHAR_MIN
    # ifdef CHAR_SIGNED
    #  define CHAR_MIN SCHAR_MIN
    # else
    #  define CHAR_MIN 0u
    # endif
    #endif
    Full project here: fbstdc

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    738
    Here's a problem:

    Code:
    #ifndef CHAR_SIGNED
    # if defined( __CHAR_UNSIGNED__ )
    # elif defined( __CHAR_SIGNED__ )
    #  define CHAR_SIGNED
    # elif ('\0'|(1 << (CHAR_BIT-1))) < 0
    #  define CHAR_SIGNED
    # endif
    #endif
    More specifically this line:

    Code:
    # elif ('\0'|(1 << (CHAR_BIT-1))) < 0
    will probably not work as intended because a character literal like '\0' has type int, not char (though I don't know if the preprocessor follows the same rules as the compiler proper).

    Besides, when would you ever care whether char is signed or unsigned? If you need an unsigned or signed char, use unsigned char or signed char (or use one of the standard types like uint8_t or int8_t if you need a small numeric type). Otherwise, use a plain char.

    If you insist on having macros for this, how about this:

    Code:
    #define CHAR_SIGNED ((char)-1 < 0)
    #define CHAR_UNSIGNED (!CHAR_SIGNED)
    which are evaluated by the compiler proper (any halfway-decent compiler from the last 30 years will very likely optimize them out because they're compile-time constants).

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    I don't understand what the '\0' is for in
    Quote Originally Posted by awsdert View Post
    # elif ('\0'|(1 << (CHAR_BIT-1))) < 0
    Putting 0 on the left hand side of a bitwise or makes no sense (because you may as well no have the bitwise or at all) and might make the reviewer (me in this case) think that the programmer has made a typo or doesn't understand what a bitwise OR does. I'd be just as suspicious of something like if (0 + 4) < blah ... What's the 0 there for? If it's intentional then I'd expect the programmer to provide a comment

  13. #13
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by Hodor View Post
    I don't understand what the '\0' is for in


    Putting 0 on the left hand side of a bitwise or makes no sense (because you may as well no have the bitwise or at all) and might make the reviewer (me in this case) think that the programmer has made a typo or doesn't understand what a bitwise OR does. I'd be just as suspicious of something like if (0 + 4) < blah ... What's the 0 there for? If it's intentional then I'd expect the programmer to provide a comment
    I needed the character but am trying to keep it from being promoted to an integer, that was the best I could come up with, I also figured it might not work so I asked here, it's a very rare situation that it would actually be used but still want to make sure it works, if you got a better idea that doesn't rely on CHAR_MAX & CHAR_MIN I'm all eyes

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Quote Originally Posted by christop View Post
    If you insist on having macros for this, how about this:

    Code:
    #define CHAR_SIGNED ((char)-1 < 0)
    #define CHAR_UNSIGNED (!CHAR_SIGNED)
    which are evaluated by the compiler proper (any halfway-decent compiler from the last 30 years will very likely optimize them out because they're compile-time constants).
    The problem is these macros need to be used in the precompiler and from experience the #if/#elif treat casts as an error meaning I cannot use (char) as much as I would like to

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by awsdert
    I needed the character but am trying to keep it from being promoted to an integer, that was the best I could come up with
    christop's correct observation that '\0' is of type int aside, in standard C, you cannot stop the integer promotions from getting applied when the operand is of integer type with a lower rank than int or unsigned int, including the subset of character types. This is true for -, <<, |, and pretty much all operators that operate on integers (other than sizeof and the address-of operator, and possibly a few others not applicable here).

    Quote Originally Posted by awsdert
    The problem is these macros need to be used in the precompiler and from experience the #if/#elif treat casts as an error meaning I cannot use (char) as much as I would like to
    Would you happen to have an example of such code and the corresponding error?
    Last edited by laserlight; 08-12-2020 at 12:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick program check
    By Nik635 in forum C Programming
    Replies: 1
    Last Post: 09-17-2015, 08:20 AM
  2. Quick check
    By SofaWolf in forum C Programming
    Replies: 6
    Last Post: 06-26-2012, 12:53 PM
  3. potential problems?
    By deepcode in forum C Programming
    Replies: 4
    Last Post: 08-11-2010, 02:04 PM
  4. Need a quick check
    By Aliaks in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2009, 04:57 AM
  5. Quick check on reading in + using strings
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 12-08-2008, 10:12 PM

Tags for this Thread