It's UWAweek 47

help3007

This forum is provided to promote discussion amongst students enrolled in CITS3007 Secure Coding. If posting a question, it's suggested you check first whether your question is answered in the unit Frequently Asked Questions (FAQ) list, and use the search box (on the right) to see if an answer to your question has already been posted.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Note that any posts must comply with the UWA Code of Conduct and the UWA Academic Conduct Policy. That means you should (a) treat everyone with respect and courtesy, and (b) not post your solutions to an assessment that's in progress.

If asking a programming question, it's recommended you read How do I ask a good question? If reporting or troubleshooting a bug in software used in the unit, it's recommend you read How to report bugs effectively.
Displaying the 2 articles in this topic
Showing 2 of 170 articles.
Currently 27 other people reading this forum.


 UWA week 19 (1st semester, week 10) ↓
SVG not supported

Login to reply

👍?
helpful

I was trying to run this section of the code from the project outline.

This is the code I was trying to run:

char plain_text[] = "HELLOWORLD";
char cipher_text[sizeof(plain_text)] = {0};
caesar_encrypt('A', 'Z', 3, plain_text, cipher_text);

I compiled the program using this command.

cc -std=c11 -Wall -Werror -o e_cits3007_project cits3007_project.c

But I was getting these warnings:

cits3007_project.c: In function ‘main’:
cits3007_project.c:29:5: error: variable-sized object may not be initialized
   29 |     char cipher_text[strlen(plain_text)] = {0};
      |     ^~~~
cits3007_project.c:29:45: error: excess elements in array initializer [-Werror]
   29 |     char cipher_text[strlen(plain_text)] = {0};
      |                                             ^
cits3007_project.c:29:45: note: (near initialization for ‘cipher_text’)

From my reading online about these warnings, I read that "You cannot initialize a variable length array in C" (I got that from this link: https://stackoverflow.com/questions/14186879/c-error-variable-sized-object-may-not-be-initialized). I know that I can get around this warning by allocating the memory or something like that, but the fact that this is from the project outline has me worrying!

I am not that confident with my C coding so I am a little paranoid about the warnings as the code is straight from the project outline.

Is this wrong? Am I wrong (probably)? If I am wrong, why so?

Hope you guys can help! :D


SVG not supported

Login to reply

👍?
helpful

Hi,

A couple of things.

The reason you're getting warnings is because the code you're compiling is not, in fact, from the spec. Check the compiler messages carefully! You've said you're trying to compile this:

  // fragment A
  char cipher_text[sizeof(plain_text)] = { 0 };

Which is correct, and will compile with no warnings. However, as the compiler warnings clearly indicate, what's in your code, and what you're actually compiling, is this:

  // fragment B
  char cipher_text[strlen(plain_text)] = { 0 };

Which will never work* - strlen is evaluated at run-time, and C requires we know the size of arrays at compile-time. (Note that sizeof isn't actually a function call, though it might look like one: it's a built-in operator, and gets evaluated at compile-time, not run-time.)

So: it's good that you're using compiler warning flags, and reading the warnings. You just need to read them a little more closely :) (btw, chatGPT is usually reasonably good at explaining warnings, if you paste in both your code and the warning messages. Talking to teaching staff in the labs is ultimately more reliable, but chatGPT has "seen" lots of C code so mostly gives good answers. If you use a rarer language like Haskell or Nim or Zig, it gives fairly bad ones.)

A couple of side-notes:

  • For the exam, remember to be careful with your use of terminology. "Run" is not the same as "compile", and confusing the two ("I was trying to run this section of the code") will result in loss of marks in exam questions - markers will assume you don't understand the difference.

  • You should be adding -pedantic-errors as a flag. That's what the labs and FAQ say to use - as I mentioned in this week's lecture, it turns off GCC-specific extensions to C.

  • I recommend checking your code into version control using git. (See the FAQ, I think it mentions Git.) When trying to reproduce a problem: ensure your code is checked in, clone the whole project to a fresh directory, and run the tests/make commands again. This ensures your code is in a known state, and you can show the checked-in version to teaching staff.

    If you don't use Git, you'll eventually confuse yourself, as has happened here: you'll lose track of what's really in your codebase, and have trouble reproducing problems.

  • It doesn't make a difference on our version of Linux, but I'd get in the habit of invoking gcc rather than cc. It's always best to know exactly what compiler you're using. It's unlikely unless you've done something odd to Ubuntu, but it's possible for cc and gcc to refer to different versions of GCC, or even completely different compilers, and potentially produce different results when compiling.

I hope that helps! Feel free to follow up if anything is unclear.

Cheers

Arran

 

* To be precise, C11 does have something called "variable-length arrays", but they have a number of traps for the unwary, and many projects (e.g. Linux) prohibit their use.

ANONYMOUS wrote:

I was trying to run this section of the code from the project outline.

This is the code I was trying to run:

char plain_text[] = "HELLOWORLD";
char cipher_text[sizeof(plain_text)] = {0};
caesar_encrypt('A', 'Z', 3, plain_text, cipher_text);

I compiled the program using this command.

cc -std=c11 -Wall -Werror -o e_cits3007_project cits3007_project.c

But I was getting these warnings:

cits3007_project.c: In function ‘main’:
cits3007_project.c:29:5: error: variable-sized object may not be initialized
   29 |     char cipher_text[strlen(plain_text)] = {0};
      |     ^~~~
cits3007_project.c:29:45: error: excess elements in array initializer [-Werror]
   29 |     char cipher_text[strlen(plain_text)] = {0};
      |                                             ^
cits3007_project.c:29:45: note: (near initialization for ‘cipher_text’)

From my reading online about these warnings, I read that "You cannot initialize a variable length array in C" (I got that from this link: https://stackoverflow.com/questions/14186879/c-error-variable-sized-object-may-not-be-initialized). I know that I can get around this warning by allocating the memory or something like that, but the fact that this is from the project outline has me worrying!

I am not that confident with my C coding so I am a little paranoid about the warnings as the code is straight from the project outline.

Is this wrong? Am I wrong (probably)? If I am wrong, why so?

Hope you guys can help! :D

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  8:08AM Aug 25 2024
Privacy policy